首頁  >  文章  >  web前端  >  Js Jq取得URL參數的集中方法範例程式碼_javascript技巧

Js Jq取得URL參數的集中方法範例程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 16:47:381399瀏覽

JQ取值方法:

jquery本身也不存在取得URL參數的方法,但是已經存在插件,可以直接取得URL等參數
插件連接主頁:https://github.com/allmarkedup/ jQuery-URL-Parser
下載連結:http://download.github.com/allmarkedup-jQuery-URL-Parser-bb2bf37.zip

Examples of use
Using the current page's url ( for these examples https://mysite.com/information/about/index.html?itemID=2&user=dave):

// get the protocol
jQuery.url.attr("protocol") // returns 'http'

// get the path
jQuery.url.attr("path") // returns '/information/about/index.html'


// get the host
jQuery.url.attr("host") // returns 'mysite.com'

// get the value for the itemID query parameter
jQuery.url .param("itemID") // returns 2

// get the second segment from the url path
jQuery.url.segment(2) // returns 'about'
Using a different url to the current page:

// set a different URL and return the anchor string
jQuery.url.setUrl("http://allmarkedup.com/category/javascript/#footer"). attr("anchor") // returns 'footer'

JS原生取得:

最原始的JS方法:

複製程式碼 程式碼如下:

var URLParams = new Array();
var aParams = document.location.search.substr(1).split(' &');
for (i=0; i var aParam = aParams[i].split('=');
URLParams[aParam[0]] = aParam[1];
}

這樣呼叫:

http://127.0.0.1/index.php?name=name1&cid=123
//取得傳過來的name參數
name=URLParams["name"];

document.write(name);
//取得傳過來的cid

cid=URLParams ["cid"];

正則分析法:

方法一:
複製代碼 程式碼如下:

function getQueryString(name) {
var reg = new RegExp("(^|&)" name "=([^&]*)(&|$) ", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null ;
}


這樣調用:
複製代碼 代碼如下:

alert(GetQueryString("參數名稱1"));

alert(GetQueryString("參數名稱2"));

alert(GetQueryString("參數名稱3"));

方法二:
複製程式碼 程式碼
複製程式碼


程式碼



}
return theRequest;
}
複製程式碼


程式碼如下:


參數2 = Request['參數2']; 參數3 = Request['參數3']; 參數N = Request['參數N'];
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn