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?
3 Answers 3
document.location.search
returns everything following ?
.
-
Including the
?
, according to MDN (source: developer.mozilla.org/en-US/docs/Web/API/URLUtils.search).David Thomas– David Thomas2014年06月20日 21:11:42 +00:00Commented Jun 20, 2014 at 21:11 -
Yes, including
?
just tested. Thanks guysAdjit– Adjit2014年06月20日 21:12:20 +00:00Commented Jun 20, 2014 at 21:12
You could try using just window.location.href
var pathname = window.location.href;
console.log(pathname);
console.log(window.location.href);
-
Please be sure to answer the question. Provide details and share your research! Asking for help, clarification, or responding to other answersWhitecat– Whitecat2019年12月26日 12:24:39 +00:00Commented Dec 26, 2019 at 12:24
window.location.search
?