Current location - Quotes Website - Collection of slogans - The problem of JS getting parameters after html address bar
The problem of JS getting parameters after html address bar
The Method of Obtaining Address Bar Parameters with JS (Super Simple)

Method 1: Use regular expressions to get address bar parameters: (highly recommended, practical and convenient! )

Function GetQueryString (name)

{

Var reg = new regexp ("(|&; ) "+name+"= ([&; ]*)(& amp; |$)");

var r = window . location . search . substr( 1)。 Matching (reg);

if(r! =null) returns unescape (r [2]); Returns null

}

//Call the method

Alert(GetQueryString ("parameter name1"));

Alert(GetQueryString ("parameter name 2"));

Alert(GetQueryString ("parameter name 3"));

Here's an example:

If the address bar is: abc.html? id = 123 & amp; url=

Then, it is called by the above method: alert (get query string ("URL"));

A dialog box will pop up: the content is

If you use: alert (get query string ("ID")); Then the pop-up content is123;

Of course, if you don't pass parameters, such as the address is abc.html, and there are no parameters, then sometimes it will be wrong to forcibly output the call result:

Therefore, a judgment is added to judge whether the requested parameter is empty. First, assign values to variables:

var myurl = getquery string(" URL ");

If (myurl! = null & amp& ampmyurl.toString()。 Length & gt 1)

{

alert(getquery string(" URL "));

}

So you won't make mistakes!

Method 2: Traditional method

& ltscript type = " text/JavaScript " & gt;

Function UrlSearch ()

{

Var name, value;

Varstr = location.href// gets the entire address bar.

var num=str.indexOf("? )

str = str . substr(num+ 1); //Get all parameters stringvar.substr (start [,length]).

var arr = str . split(& amp; ); //Put each parameter into an array.

for(var I = 0; I < array length; i++){

num=arr[i]。 index of(" = ");

if(num & gt; 0){

name=arr[i]。 Substring (0, num);

value=arr[i]。 substr(num+ 1);

This [name] = value;

}

}

}

var Request = new UrlSearch(); //instantiation

alert(request . id);

& lt/script & gt;

For example, save this code as 1.html.

So you want to visit 1.html? Id = test

At this point, the value of test is obtained.

Call in html

& ltscript type = " text/JavaScript " & gt;

var a =

& lt/script & gt;

& lt/head & gt;

& ltbody & gt

& lta id = " a 1 " href = " " & gt; sadfsdfas & lt/a & gt;

& lt script & gt

var a 1 = document . getelementbyid(" a 1 ");

a 1 . href = a;

& lt/script & gt;

& ltscript type = " text/JavaScript " & gt;

var a="/gg.htm? CCTV ";

var s=a.indexOf("? );

var t = a . substring(s+ 1); // t is it? There's something back there.

& lt/script & gt;

Stringvar.substr (start [,length]

Returns a substring of the specified length starting at the specified position.

String variable

Required option. String literal or string object to extract substring.

begin

Required option. The starting position of the required substring. The index of the first character in the string is 0.

length

Optional. The number of characters that should be included in the returned substring.

If the length is 0 or negative, an empty string will be returned. If this parameter is not specified, the substring will continue to the end of stringvar.

The following are some relevant parameters:

Str.toLowerCase () is converted to lowercase.

The string str.toUpperCase () is all converted to uppercase.

A URL is a uniform resource locator (URL).

A complete URL consists of the following parts:

scheme://host:port/path? Query # fragment

Scheme: communication protocol

Commonly used /index.html? ver= 1.0。 Id = 6 # Here it is.

You can get all the parts in javascript.

1,window.location.href

The complete URl string (the complete address bar in the browser)

The return value of this example: /index.html? ver= 1.0。 Id = 6 # Here it is.

2. Window. Location. Protocol

Protocol part of URL

The return value of this example:

4、window.location.port

Port part of URL

If the default port 80 is adopted (update: even if 80 is added), the return value is not the default 80 but a null character.

The return value of this example: ""

5. Window. Location. Pathname

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

The return value of this example:/fisker/post/0703/window.location.html.

6. Window. Location. Search

Query (parameter) section

In addition to assigning values to dynamic languages, you can also assign values to static pages and use javascript to obtain corresponding parameter values.

The return value of this example:? ver= 1.0。 id=6

7, window.location

Anchor point

The return value of this example: #imhere