js obtain parameters page get jump

Get the page jump parameters js

Window.location.href page by page or jump through window.parent.location.href, how to obtain the corresponding parameters in the new page it?

window.location.href way

Which remove the "#" sign is because the url parameter also added a # parameter.

function GetRequest(name) {
    var url = window.location.search; //获取url中"?"符后的字串
    // var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        if(str.indexOf("#" != -1)){
            str = str.substr(0);
        }
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            if(strs[i].indexOf(name) != -1){
                return strs[i].split("=")[1];
            }
        }
    }
    return null;
}

window.parent.location.href

function GetRequest(name) {
    var url = window.parent.location.search; //获取url中"?"符后的字串
    // var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        if(str.indexOf("#" != -1)){
            str = str.substr(0);
        }
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            if(strs[i].indexOf(name) != -1){
                return strs[i].split("=")[1];
            }
        }
    }
    return null;
}

This difference between the two places is to get the url, keeping up with the way the page jump.

other methods

The network also provides other ways, can be obtained above with reference to the corresponding modified url manner.

function GetRequest() {
 var url = window.location.search; //获取url中"?"符后的字串
 var theRequest = new Object();
 if (url.indexOf("?") != -1) {
     var str = url.substr(1);
     strs = str.split("&");
     for(var i = 0; i < strs.length; i ++) {
         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
     }
 }
 return theRequest;

Original link: https://www.choupangxia.com/2019/08/04/js%e8%8e%b7%e5%be%97%e9%a1%b5%e9%9d%a2get%e8%b7%b3 % e8% bd% ac% e7 % 9a% 84% e5% 8f% 82% e6% 95% b0 /

Guess you like

Origin www.cnblogs.com/secbro/p/11297889.html