How to get the parameters in the URL using JS

Source: Internet
Author: User

1. Get the entire URL string

To get the parameters in the URL, we first get to the entire URL string. We take http://localhost:8080/Charge/homePage.html?costInfoId=1 as an example

① gets (or sets) the protocol part of the URL: Window.location.protocol

var test = Window.location.protocol;  alert (test);  Back popup: http:  

② gets (or sets) the host part of the URL: Window.location.host

var test = Window.location.host;  alert (test);  

③ gets (or sets) the URL associated with the port number: Window.location.port

var test = Window.location.port;  alert (test);  Back popup: 8080 (if the default 80 port is added (even if: 80), the return value is not the default 80 but the null character)

④ gets (or sets) the path part of the URL is also the file address: Window.location.pathname

var test = Window.location.pathname;  alert (test);  

⑤ gets (or sets) the part of the URL property that follows the question mark: Window.location.search

var test = Window.location.search;  alert (test);  Back to popup:? costinfoid=1

⑥ gets (or sets) the fragment of the URL attribute after the pound sign "#": Window.location.hash

var test = Window.location.hash;  alert (test);  Return popup: null character (not in URL)  

⑦ gets (or sets) the entire URL string: window.location.href

var test = window.location.href;  alert (test);  Back popup: Http://localhost:8080/Charge/homePage.html?costInfoId=1

2. Get the parameter values in the URL

After getting the URL string, you get the parameter data information in the URL string. Here are a few ways to get parameters:

① with regular expressions to get the parameter values

function getquerystring (name) {      varnew RegExp ("(^|&)" + name + "= ([^&]*) (&|$)"  );       var r = window.location.search.substr (1). Match (reg);       if (r!=nullreturnreturn ';  }  

②split Splitting method

functiongetrequest () {varurl = location.search;//gets the URL in the "?" String after the character    varTherequest =NewObject (); if(Url.indexof ("?")! =-1) {          varstr = URL.SUBSTR (1); STRs= Str.split ("&");  for(vari = 0; i < strs.length; i++) {Therequest[strs[i].split ("=") [0]] = unescape (strs[i].split ("=") [1]); }      }      returntherequest; }  varRequest =NewObject (); Request=getrequest (); //var id=request["id"]; //var parameter 1, Parameter 2, parameter 3, parameter n; //parameter 1 = request[' parameter 1 ']; //Parameter 2 = request[' parameter 2 ']; //Parameter 3 = request[' parameter 3 ']; //parameter n = request[' parameter n ']; 

③ method of obtaining a single parameter

function getrequest () {      var// get url "?" Character  string    if (Url.indexof ("?")! =-1) {  // to determine if there is a  parameter        var  // start with the first character because the No. 0 one is the number. Gets all string          STRs = str.split ("=") except the question mark;  // separated by an equal sign (because you know that there is only one argument so you can separate it with an equal sign if you have more than one argument  separated by an equal sign &)        Alert (strs[1]);     // directly pops the first parameter (if more than one parameter is to be looped)       }  }  

 

Use JS to get the parameters in the URL method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.