今天写项目需要获取url后面的参数ref参数来判断是否开启计时器来刷新页面,之前一直都是用JS写的,今天在查资料的时候看到了一款JQ的插件
项目地址:https://github.com/allmarkedup/purl
官网说明(英文不好,我翻译直译的)
Specifying the URL to parse
有几个不同的方式来选择什么网址解析:
/*---- jQuery version -----*/
var url = $.url(); // parse the current page URL
var url = $.url('http://allmarkedup.com'); // pass in a URI as a string and parse that
var url = $('#myElement').url(); // extract the URL from the selected element and parse that - will work on any element with a `src`, `href` or `action` attribute.
/*---- plain JS version -----*/
var url = purl(); // parse the current page URL
var url = purl('http://allmarkedup.com'); // pass in a URI as a string and parse that
URL attributes
用attr()方法用于返回的URL不同部分的信息。例如:
var url = $.url('http://allmarkedup.com/folder/dir/index.html?item=value'); // jQuery version
var url = purl('http://allmarkedup.com/folder/dir/index.html?item=value'); // plain JS version
url.attr('protocol'); // returns 'http'
url.attr('path'); // returns '/folder/dir/index.html'
Query 参数化解析
/*---- jQuery version -----*/ $.url('http://allmarkedup.com?sky=blue&grass=green').param('sky'); // returns 'blue' /*---- plain JS version -----*/ purl('http://allmarkedup.com?sky=blue&grass=green').param('sky'); // returns 'blue'
我今天用到的是Query 参数化解析,上代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript" src="js/purl.js"></script> </head> <body> <script type="text/javascript"> $(function () { var url = window.location.href; var ref = purl(url).param("ref"); alert(ref); }); </script> </body> </html>