JavaScript gets the previous page hyperlink URL parameter

Source: Internet
Author: User

The core is the regular

The code is as follows Copy Code

var url = location.href;
Gets the parameter value of the keyword
var es=/keyword=/;
Es.exec (URL);
var Result=regexp.rightcontext;


For example:

The code is as follows Copy Code


var url = location.href;
Get parameter values for PID
var es=/pid=/;
Es.exec (URL);
var Pid=regexp.rightcontext;

var aurl = "&pid=" +PID;


alert (PID);//Result: &pid=37

example, we operate with & as a delimiter

Example

The code is as follows Copy Code
function Getargs () {
var args = new Object ();
var query = location.search.substring (1); Get query string
var pairs = Query.split ("&"); Break at Ampersand
for (var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexof (' = '); Look for "Name=value"
if (pos = = 1) continue; If not found, skip
var argname = pairs[i].substring (0,pos); Extract the name
var value = pairs[i].substring (pos+1); Extract the value
Value = decodeuricomponent (value); Decode it, if needed
Args[argname] = value; Store as a property
}
return args; Return the object
}

And then set the location value based on the parameters you get.

This is the current page is actually the same as a page, and of course you can also use Document.referrer

The code is as follows Copy Code

<script type= "Text/javascript" >
var url = window.location;
function Geturlparam (url,name) {
var pattern = new RegExp ("[?]" + Name + "\= ([^&]+)", "G");
var matcher = pattern.exec (URL);
var items = null;
if (Matcher!= null) {
try{
Items = decodeURIComponent (decodeURIComponent (matcher[1]));
}catch (e) {
try{
Items = decodeURIComponent (matcher[1]);
}catch (e) {
Items = matcher[1];
}
}
}
return items;
}

Alert (Geturlparam (URL, ' type ')); Country
Alert (Geturlparam (URL, ' id ')); 12
Alert (Geturlparam (URL, ' page ')); 10

</script>

Code to optimize the

  code is as follows copy code

var url = " Www.111cn.net Aada=adaa&adad=adasd&sdfs=asdad#did ",
Reg =/([^\=\?| \&]+) \= ([^\=\&\#]+)/g;
var aa = Url.match (reg);

Console.log (AA);
Url.replace (reg,function ($1,$2,$3) {
Console.log ($2+ "=" +$3);
});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.