JS method to get page URL

Source: Internet
Author: User

we can use Javas . Cript get all the parts

1, Window.location.href

The entire URL string (in the browser is the full address bar)

This example returns the value: http://ifisker.com/blog/post/0703/window.location.html#

2,window.location.protocol

The protocol portion of the URL

This example returns the value: http:

3,window.location.host

The host part of the URL

This example returns the value: Www.x2y2.com

4,window.location.port

The port portion of the URL

If you are using the default 80 port (update: Even if you added: 80), the return value is not the default of 80 but the null character

This example returns the value: ""

5,window.location.pathname

The path portion of the URL (that is, the file address)

This example returns the value:/fisker/post/0703/window.location.html

6,window.location.search

Query (Parameters) Section

in addition to assigning values to dynamic languages, we can also give static pages and use Javas cript to get the parameter value of the phase letter

This example returns the value:? ver=1.0&id=6

7,window.location.hash

Anchor Point

This example returns the value: #imhere

Parameters of URL & parameter name 1=xxxx& parameter name 2=xxxx& parameter name 3=xxxx

function     getquerystring (name)      {          var    reg   =   new    RegExp ("(^|& ) "+    name   +" = ([^&]*) (&|$) ");           var    R   =    window.location.search.substr (1). Match (reg);          if    (r!=null)   return    (R[2]);   return   NULL ;      }      Alert (getquerystring ("parameter name 1"));      Alert (getquerystring ("parameter name 2"));      Alert (getquerystring ("parameter name 3"));

such as: http://www.cnblogs.com/EditPosts.aspx?a=1&b=2&c=3

b = getquerystring ("B"); [b=2]

Convert to Digital parseint (NUM)

function   Geturlparam (name) {            var   reg   =   new   RegExp ("(^|&) "+   name   +" = ([^&]*) (&|$) ");             var   R   =   window.location.search.substr (1). Match (reg);             if   (r!=null)   return   (R[2]);   return   NULL ;      }

If aa.aspx?id=3

Call only Geturlparam ("id") to get 3

For example, I want to use JS to intercept this link http://www.abc.com/?id=9&keywords=milk&sid=1
This section of the Keywords=milk, and each time this paragraph will be connected with the change, such as Keywords=orange interception, it is necessary to pay a value to a variable
var = "Keywords=orange";

 var url= "http://www.abc.com/?id=9&keywords=apple&sid=1"; 
P=geturlparams (URL);
var v= "keywords=" +p["keywords"]
alert (v);

function geturlparams (URL) // Gets the page URL parameter { var urlparams = new Object (); var aparams = Url.split (' & ' for (i=0; i < aparams.length; I++ var Aparam = aparams[i].split (' = ' ); urlparams[aparam[ 0]] = Aparam[1];} return Urlparams;} </script>

JS method to get page 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.