【js】url中的一些操作总结

一、获取整个url

window.location.href  // http://localhost:8060/list.html?nav=6&index=1&iframe=%2Fenterprise%2Findex

二、获取问号后面的

window.location.search //?nav=6&index=1&iframe=%2Fenterprise%2Findex

三、获取url参数

  

var getParameter = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
    var r = location.search.substr(1).match(reg);
    if (r!=null) return (r[2]); return null;
}
var search = location.search; 
var nav = getParameter('nav');//6

四、获取url中的域名

window.location.host  // localhost:8060

五、获取url中的端口号

window.locatiom.port

六、获取urkl中的hash

window.location.hash

七、获取url中的协议

window.location.protocol      //http:

八、将参数值进行编码解码

例如:

编码:var str = '/enterprise/index';

console.log(encodeURIComponent('/enterprise/index')) );    //iframe=%2Fenterprise%2Findex

var iframe =  getParameter ('iframe ');

解码:window.decodeURIComponent(iframe );

原文地址:https://www.cnblogs.com/websmile/p/13724011.html