I have the following URL in the browser address bar
http://localhost:8080/MyApp/MyScreen?userName=ccc
I need to get the part /MyScreen?userName=ccc
from the it, excluding the root.
How can i get this using jQuery?
I have the following URL in the browser address bar
http://localhost:8080/MyApp/MyScreen?userName=ccc
I need to get the part /MyScreen?userName=ccc
from the it, excluding the root.
How can i get this using jQuery?
There isn't much built in for this, as your app and a browser won't actually agree on what the "root" is.
To a browser, /MyApp/
is just another directory name under the root, which it's convinced is:
http://localhost:8080/
However, if you can get a "base" URL from your application:
var baseUrl = "http://localhost:8080/MyApp";
You can then .replace()
that from the current href
:
var pagePath = window.location.href.replace(baseUrl, "");
console.log(pagePath);
// "/MyScreen?userName=ccc"
Example, using a mock location
object: http://jsfiddle.net/CWcvW/
var a = location.pathname + location.search
If for some reason you also want the hash also (the #
part of the url), use this instead:
var a = location.pathname + location.search + location.hash
Then, you must remove your app root path from a
:
a = a.replace( "/MyApp/", "" );
// or
a = a.substring( "/MyApp/".length );