我在浏览器地址栏中有以下网址
http://localhost:8080/MyApp/MyScreen?userName=ccc
我需要从中获取部分/MyScreen?userName=ccc
,不包括根。
我如何使用jQuery获得这个?
答案 0 :(得分:3)
内置的内容并不多,因为您的应用和浏览器实际上并不同意“ root ”是什么。
对于浏览器,/MyApp/
只是根目录下的另一个目录名称,它确信:
http://localhost:8080/
但是,如果您可以从应用程序中获取“ base ”网址:
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/
答案 1 :(得分:1)
var a = location.pathname + location.search
如果出于某种原因你也想要哈希(网址的#
部分),请改用:
var a = location.pathname + location.search + location.hash
然后,您必须从a
删除您的应用根路径:
a = a.replace( "/MyApp/", "" );
// or
a = a.substring( "/MyApp/".length );