日期:2014-05-16  浏览次数:20374 次

怎样获取当前url某字段


例如当前页面地址是  http://localhost/OA/WorkSystem/MessagePublish/KnowledgeList.aspx?KnowledgeType=img&contentStr=%b2%fa%c6%b7%cd%bc%c6%ac%cb%d1%cb%f7


我想用js获取以上url中 KnowledgeType= 后面的字段  省略&contentStr=%b2%fa%c6%b7%cd%bc%c6%ac%cb%d1%cb%f7

应该怎样写

------解决方案--------------------
window.location.search可以获取当前的url的参数
//var url = window.location.search
var url = "?KnowledgeType=img&contentStr=%b2%fa%c6%b7%cd%bc%c6%ac%cb%d1%cb%f7&_KnowledgeType=xxx";
var reg = new RegExp("(?:\\?KnowledgeType
------解决方案--------------------
&KnowledgeType)=(.*?)(?=$
------解决方案--------------------
&)");
console.log(reg.exec(url)[1]);


var url = "?contentStr=%b2%fa%c6%b7%cd%bc%c6%ac%cb%d1%cb%f7&KnowledgeType=xxx&__KnowledgeType=aaaa";
var reg = new RegExp("(?:\\?KnowledgeType
------解决方案--------------------
&KnowledgeType)=(.*?)(?=$
------解决方案--------------------
&)");
console.log(reg.exec(url)[1]);

------解决方案--------------------
window.location.href获得地址后截试试