JavaScript Get URL and URL Parameters

0
279
get-url-parameter-in-js

Welcome back to shortlearner.com, in this post we will see how get URL and URL parameters with the help of JavaScript functionality. People also asked how to Get the values from the “GET” parameters (JavaScript),
Most of the time we need to fetch the parameters from URL.

get-url-parameter-in-js
get-url-parameter-in-js

so here we are taking a URL with having some parameters and will separate the param from the URL with some pre defined JavaScript functions.
JavaScript can access the current URL in parts. For this Link:

http://shortlearner.com/tool/f.php?id=63
window.location.protocol = "http:""
window.location.host = "shortlearner.com"
window.location.pathname = "/tool/f.php"
window.location.search = "?id=63"

So to get the full Website address path in JavaScript:

var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname + window.location.search;

If you need to break up up the pathname, for example, a full website address like https://shortlearner.com/blah/blah/blah/index.php, you can split the string on “/” characters.

var pathArray = window.location.pathname.split('/');

Then access the different parts by the parts of the array, like

var secondLevelLocation = pathArray[0];

To put that pathname back together, you can stitch together the array and put the “/” back in:

var newPathname = "";
for (i = 0; i < pathArray.length; i++) {
  newPathname += "/";
  newPathname += pathArray[i];
}

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript