JS gets each parameter of the URL.

Source: Internet
Author: User

JavaScript gets the two methods of the current URL parameter:

  Returns a string as a parameter, such as:class_id=3&id=2&

function geturlargstr() {

var q=location. Search. Substr(1);        

var qs=q. Split(' & ');        

var argstr=';       

if(qs) {

  For(var i=0; I<Qs. Length; I+ +) {

 Argstr+=Qs[I].Substring (0,qs[ iindexof ( ' = ' ' = ' +qs[ isubstring (qs[iindexof ( ' = ' ) +< Span class= "lit" >1) + ' & ' ;  

        }     

  }     

  return argstr;

Returns the parameter as an object form

function geturlargobject() {

var args=new Object();     

  var query=location. Search. Substring(1); Get query string

< Span class= "pun" >< Span class= "pun" >      var pairs= query.//disconnects at comma

 For (var i=0; I<pairs. Length; I+ +) {         

    var pos=pairs[i]. IndexOf(' = '); Find Name=value

< Span class= "pun" >< span class= "str" >       if (pos ==-1) {// Skip   if not found;             

continue;              

  }         

  var argname=pairs[i]. Substring(0,pos); Extract name

var value=pairs[i]. Substring(pos+1); Extract value

args[argname]=unescape(value); Save As Property

  }    

  return args; Return object

}

Also list some JavaScript to get a functional approach to each part of the URL:

  1. Window. Location.  Host; //Returns the host part of the URL, for example: www.xxx.com
  2. Window. Location.  hostname; //Return to www.xxx.com
  3. Window. Location.  href; //Returns the entire URL string (in the browser is the full address bar), for example : www.xxx.com/index.php?class_id=3&id=2
  4. Window. Location.  Pathname; //Return to/a/index.php or /index.php
  5. Window. Location.  Protocol; //Returns the protocol part of the URL, for example: Http:,ftp:,maito: And so on.  
  6. Window. Location. the port portion of Ports//url, if the default 80 port is used, then the return value is not the default of 80 but the null character

JS gets each parameter of the URL.

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.