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?

1
  • Look into the location object. Commented Aug 7, 2013 at 11:41

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

Comments

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 );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.