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 ?.
2 Comments
?, according to MDN (source: developer.mozilla.org/en-US/docs/Web/API/URLUtils.search).? just tested. Thanks guysYou could try using just window.location.href
var pathname = window.location.href;
console.log(pathname);
Comments
console.log(window.location.href);
window.location.search?