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,932
  • 7
  • 61
  • 93
user1016403
  • 12,151
  • 35
  • 108
  • 137

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
  • 121,453
  • 34
  • 200
  • 199
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
  • 40,997
  • 14
  • 121
  • 129