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?

asked Jun 20, 2014 at 21:06
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 ?.

answered Jun 20, 2014 at 21:10
2
1

You could try using just window.location.href

var pathname = window.location.href;
console.log(pathname);
answered Jun 20, 2014 at 21:09
0

console.log(window.location.href);

answered Dec 26, 2019 at 5:02
1
  • Please be sure to answer the question. Provide details and share your research! Asking for help, clarification, or responding to other answers Commented Dec 26, 2019 at 12:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.