我在浏览器地址栏中使用jQuery从URL获取字符串的一部分?
http://localhost:8080/MyApp/MyScreen?userName=ccc
我需要从它那里得到了部分/MyScreen?userName=ccc
,不包括根下面的网址。
我怎样才能得到这个使用jQuery?
我在浏览器地址栏中使用jQuery从URL获取字符串的一部分?
http://localhost:8080/MyApp/MyScreen?userName=ccc
我需要从它那里得到了部分/MyScreen?userName=ccc
,不包括根下面的网址。
我怎样才能得到这个使用jQuery?
var a = location.pathname + location.search
如果由于某种原因,你也想的哈希也(URL的#
部分),而不是使用:
var a = location.pathname + location.search + location.hash
然后,你必须从a
您的应用根路径:
a = a.replace("/MyApp/", "");
// or
a = a.substring("/MyApp/".length);
没有太多内置了这一点,因为你的应用与浏览器实际上不会同意什么“根“是。
到浏览器,/MyApp/
仅仅是根,它说服下另一个目录名称是:
http://localhost:8080/
但是,如果你可以从你的应用程序获得了“基地”网址
var baseUrl = "http://localhost:8080/MyApp";
然后,您可以.replace()
,从目前href
:
var pagePath = window.location.href.replace(baseUrl, "");
console.log(pagePath);
// "/MyScreen?userName=ccc"
实施例,使用一个模拟location
对象:http://jsfiddle.net/CWcvW/
查找到'location'对象。 – Utkanos