1

Been trying to run jQuery functions based on a dynamic url but having some issues.

Say my HTML file is called myPage.html

This is the code I am using to check the URL

var pathname = window.location.pathname;
var splitPath = pathname.split("?");
var input = splitPath[splitPath.length-1];

console.log(pathname);

I am looking at the file locally, and when I put it in the browser /pathname/myPage.html my console outputs /pathname/myPage.html exactly what I was expecting.

But now, if I change the url to /pathname/myPage.html?input=yes the page loads fine, but the console still only logs /pathname/myPage.html meanwhile I would like to see input=yes

What am I doing wrong here?

2
  • Why not just use window.location.search? Commented Jun 20, 2014 at 21:08
  • @DavidThomas because I didn't know I could use that :P Commented Jun 20, 2014 at 21:10

3 Answers 3

2

document.location.search returns everything following ?.

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

2 Comments

Including the ?, according to MDN (source: developer.mozilla.org/en-US/docs/Web/API/URLUtils.search).
Yes, including ? just tested. Thanks guys
1

You could try using just window.location.href

var pathname = window.location.href;
console.log(pathname);

Comments

0

console.log(window.location.href);

1 Comment

Please be sure to answer the question. Provide details and share your research! Asking for help, clarification, or responding to other answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.