How to accurately obtain the url Information of the current page in javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces multiple methods for js to accurately obtain the url Information of the current page, including the regular expression method and split method. For more information, see WEB development, javascript is often used to obtain the url Information of the current page. Here is a summary of some of my url Information.

Below we give an example of a URL, and then get its components: http:// I .cnblogs.com/EditPosts.aspx? Opt = 1

1. window. location. href (set or retrieve the entire URL as a string)

Var test = window. location. href;
Alert (test );
Back: http:// I .cnblogs.com/EditPosts.aspx? Opt = 1

2. window. location. protocol (set or obtain the protocol Part of the URL)

Var test = window. location. protocol;
Alert (test );
Return Value: http:

3. window. location. host (set or obtain the host part of the URL)

Var test = window. location. host;
Alert (test );
Return Value: I .cnblogs.com

4. window. location. port (set or obtain the port number associated with the URL)

Var test = window. location. port;
Alert (test );
Return: NULL character (if the default port 80 is used (update: even if the port 80 is added), the return value is not the default port 80, but a null character)

5. window. location. pathname (set or obtain the path with the URL (that is, the file address ))
Var test = window. location. pathname;
Alert (test );
Back:/EditPosts. aspx

6. window. location. search (set or obtain the Section following the question mark in the href attribute)

Var test = window. location. search;
Alert (test );
Return :? Opt = 1

PS: Get the query (parameter) part. In addition to assigning values to dynamic languages, we can also give static pages and use javascript to get the expected parameter values.

7. window. location. hash (set or obtain the segment following the # In the href attribute)

Var test = window. location. hash;
Alert (test );
Return: NULL character (because the url does not exist)

8. js obtains the parameter values in the url.

I. Regular Expression

Function getQueryString (name) {var reg = new RegExp ('(^ | &)' + name + '= ([^ &] *) (& | $ )', 'I'); var r = window. location. search. substr (1 ). match (reg); if (r! = Null) {return unescape (r [2]);} return null;} // call: alert (GetQueryString ("parameter name 1 ")); alert (GetQueryString ("parameter name 2"); alert (GetQueryString ("parameter name 3 "));

Ii. split Method

Function GetRequest () {var url = location. search; // obtain "? "Var theRequest = new Object (); if (url. indexOf ("? ")! =-1) {var str = url. substr (1); strs = str. split ("&"); for (var I = 0; I <strs. length; I ++) {theRequest [strs [I]. split ("=") [0] = unescape (strs [I]. split ("=") [1]) ;}return theRequest;} var Request = new Object (); 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'];

3. Specify to retrieve

For example, a url: http:// I .cnblogs.com /? J = js. You can call the following function to obtain the value of parameter j.

Function GetQueryString (name) {var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $ )", "I"); var r = window. location. search. substr (1 ). match (reg); // get "?" in the url "? "The character string after the regular expression matches var context =" "; if (r! = Null) context = r [2]; reg = null; r = null; return context = null | context = "" | context = "undefined "? "": Context;} alert (GetQueryString ("j "));

Iv. Obtain a single parameter

Function GetRequest () {var url = location. search; // obtain "? "If (url. indexOf ("? ")! =-1) {// determine whether there is a parameter var str = url. substr (1); // since the first character is 0th? Get all strings except Question Mark strs = str. split ("="); // use the equal sign to separate (because only one parameter is known, use the equal sign to separate multiple parameters with the equal sign) alert (strs [1]); // The first parameter is displayed directly (if there are multiple parameters, it must be cyclically )}}

The above is all the content of this article. I hope you can understand how to obtain the url Information of the current page.

Related Article

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.