使用javascript获取包含GET变量的路径

Cri*_*sty 1 javascript regex url

我需要获取整个页面路径(不包括域名),以便我可以在iframe中显示它.

我目前location.pathname用来获取路径,但问题是它们可能GET在URL中出现变量.

因此对于像article.php?id=23我只article.php使用的页面一样location.pathname,因此显示的页面iframe只是一个404页面.

是否有任何函数来获取包含GET变量的路径?

Ste*_*rne 5

可能没有开箱即用的功能,没有.

但是您可以将其用作创建自己的参考:

Mozilla DOM参考

具体来说,使用window.location.pathname(如果你不想要它window.location.search,去掉前导"/")并获取查询字符串.

function whatIWant()
{
    return  window.location.pathname.substring(1) + window.location.search;
}
Run Code Online (Sandbox Code Playgroud)