JS accurate access to the current page URL address information

Source: Internet
Author: User

This article mainly introduces JS accurate access to the current page URL information of a variety of methods, including regular method, split split method, the need for friends can refer to the next

In web development, JavaScript is often used to get URL information for the current page, and here is a little summary of some of my URL information.

Here we give an example of a URL, and then get its various components: Http://i.cnblogs.com/EditPosts.aspx?opt=1

1. Window.location.href (set or get the entire URL as a string)

var test = window.location.href;
alert (test);
return: Http://i.cnblogs.com/EditPosts.aspx?opt=1

2, Window.location.protocol (set or get the protocol portion of the URL)

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

3, Window.location.host (set or get the host part of the URL)

var test = Window.location.host;
alert (test);
return: i.cnblogs.com

4, Window.location.port (set or get the port number associated with the URL)

var test = Window.location.port;
alert (test);
return: null character (if the default 80 port (update: Even if added: 80), then the return value is not the default 80 but the null character)

5, Window.location.pathname (set or get the path part of the URL (that is, the file address))
var test = Window.location.pathname;
alert (test);
return:/editposts.aspx

6. Window.location.search (set or get the section following the question mark in the href attribute)

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

PS: Get the query (parameters) section, in addition to the dynamic language assignment, we can also give a static page, and use JavaScript to obtain the value of the parameters believed.

7. Window.location.hash (set or get the fragment after the pound sign "#" in the href attribute)

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

8. JS gets the parameter value in the URL

First, the regular law

functiongetquerystring (name) {varReg =NewRegExp (' (^|&) ' + name + ' = ([^&]*) (&|$) ', ' I '); varr = Window.location.search.substr (1). Match (REG); if(r! =NULL) {    returnUnescape (r[2]); }  return NULL;}//This is called:Alert (getquerystring ("parameter name 1"))); Alert (getquerystring ("Parameter Name 2")); Alert (getquerystring ("Parameter name 3");

Split Division 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 ();<br>//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 '];

III. Designation of

For example a url:http://i.cnblogs.com/?j=js, we want the value of the parameter J, which can be called by the following function.

functiongetquerystring (name) {varReg =NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I"); varr = Window.location.search.substr (1). Match (REG);//gets the URL in the "?" Character string and regular match  varContext = ""; if(r! =NULL) Context= R[2]; Reg=NULL; R=NULL; returnContext = =NULL|| Context = = "" | | context = = "undefined"? "": Context;} Alert (getquerystring ("J"));

Four, the method of acquiring 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 is the number. Gets all strings except the question mark   STRs = str.split ("=");  // 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)   }}

The above is the whole content of this article, we hope to understand how to get the current page URL information to help.

Reprinted from: http://www.jb51.net/article/82519.htm

JS accurate access to the current page URL address information

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.