zoukankan      html  css  js  c++  java
  • js获得页面get跳转的参数

    通过js获得页面跳转参数

    页面通过window.location.href或通过window.parent.location.href进行页面跳转,在新的页面如何获得相应的参数呢?

    window.location.href方式

    其中去除“#”号是因为url参数中还添加了#的参数。

    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;
    }
    

    这两者区别的地方是获取的url,与跳转的页面方式保持一直。

    其他方式

    网络上也提供了其他方式,可参照上面对应修改获得url的方式。

    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;
    
    

    原文链接:https://www.choupangxia.com/2019/08/04/js获得页面get跳转的参数/

  • 相关阅读:
    继承
    反射
    DOS使用笔记
    [LeetCode] Merge Intervals
    [LeetCode] Insert Interval
    [LeetCode] Permutation Sequence
    [LeetCode] Rotate List
    [LeetCode] Text Justification
    [LeetCode] Simplify Path(可以不用看)
    [LeetCode] Edit Distance(很好的DP)
  • 原文地址:https://www.cnblogs.com/secbro/p/11297889.html
Copyright © 2011-2022 走看看