javascript学习指南|Javascript获取上一个页面超链接url参数

更新时间:2019-06-07    来源:word    手机版     字体:

【www.bbyears.com--word】

核心就是正则

 代码如下

var url = location.href;
//获取keyword的参数值
var es=/keyword=/;
es.exec(url);
var result=RegExp.rightContext;


例如:

 代码如下


var url = location.href;
//获取pid的参数值
var es=/pid=/;
es.exec(url);
var pid=RegExp.rightContext;

var aurl = "&pid="+pid;


alert(pid);//结果:&pid=37

例子,我们以&为分界符来操作

例子

 代码如下 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
}

然后根据获取到的参数 设置location值就可以了

这个是当前页面其实与一个页面是一样的, 当然们也可中以使用document.referrer

 代码如下

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

代码进行优化一下

 代码如下

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

本文来源:http://www.bbyears.com/bangongshuma/53565.html

热门标签

更多>>

本类排行