1

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?

ndequeker
  • 7,702
  • 7
  • 58
  • 93
user1016403
  • 11,631
  • 34
  • 103
  • 135

2 Answers2

3

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/

Jonathan Lonowski
  • 117,332
  • 31
  • 195
  • 197
1
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 );
gustavohenke
  • 39,785
  • 13
  • 117
  • 123