现在的位置: 网页制作教程JS网页特效 >正文
JavaScript脚本

js获取当前URL参数方式详解

发表于2016/10/31 JS网页特效 0条评论 ⁄ 热度 1,240℃
导语:本文介绍了通过js获取当前url的参数,分别列出了不同的获取参数,通过实例简单明了地说明了他们之间的相同和区别。

js获取URL参数主要有以下几种方式:document.URL、document.location.href、self.location.href、document.location、top.location.href、parent.document.location、top.location.hostname、location.hostname。废话不多说了,直接上代码。

js获取url参数

<script language="javascript">
document.write("<table width=100% cellpadding=0 cellspacing=0 border=0>");
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
strwrite = "<tr><td valign=top>thisURL: </td><td>[" + thisURL + "]</td></tr>"
strwrite += "<tr><td valign=top>thisHREF: </td><td>[" + thisHREF + "]</td></tr>"
strwrite += "<tr><td valign=top>thisSLoc: </td><td>[" + thisSLoc + "]</td></tr>"
strwrite += "<tr><td valign=top>thisDLoc: </td><td>[" + thisDLoc + "]</td></tr>"
document.write( strwrite );
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
strwrite = "<tr><td valign=top>thisTLoc: </td><td>[" + thisTLoc + "]</td></tr>"
strwrite += "<tr><td valign=top>thisPLoc: </td><td>[" + thisPLoc + "]</td></tr>"
strwrite += "<tr><td valign=top>thisTHost: </td><td>[" + thisTHost + "]</td></tr>"
strwrite += "<tr><td valign=top>thisHost: </td><td>[" + thisHost + "]</td></tr>"
document.write( strwrite );
tmpHPage = thisHREF.split( "/" );
thisHPage = tmpHPage[ tmpHPage.length-1 ];
tmpUPage = thisURL.split( "/" );
thisUPage = tmpUPage[ tmpUPage.length-1 ];
strwrite = "<tr><td valign=top>thisHPage: </td><td>[" + thisHPage + "]</td></tr>"
strwrite += "<tr><td valign=top>thisUPage: </td><td>[" + thisUPage + "]</td></tr>"
document.write( strwrite );
document.write("</table>");
</script>

通过以上代码,相信你在js获取url的运用上更加自如。

扩展阅读:URLRewrite组件js获取网址

  • 暂无评论