html get get the url parameter

jquery get get the url parameter

Just use the first method, simple and easy to use directly into the name of the parameter you want to get, you can return the parameter values

function GetQueryString(name)

{

     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");

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

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

}

Reads as follows

 

JS method to obtain the address bar parameters (super easy)

Method One: Using regular expressions to obtain the address bar parameters :( highly recommended, both practical and convenient)!

function GetQueryString(name)

{

     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");

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

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

}

 

// 调用方法

alert(GetQueryString("参数名1"));

alert(GetQueryString("参数名2"));

alert(GetQueryString("参数名3"));

Here is an example:

If the address bar URL is: abc.html id = 123 & url = http:? //Www.maidq.com

Well, but you use the above method to call: alert (GetQueryString ( "url"));

A dialog box will pop up: content is  http://www.maidq.com

If you use: alert (GetQueryString ( "id")); then the pop-up content is 123 friends;

Of course, if you do not pass argument, such as your address is no argument abc.html back, that forced output the result of calling sometimes will complain:

So we have to add a judgment as to our request parameter is empty, first assign a value to a variable:

var myurl=GetQueryString("url");

if(myurl !=null && myurl.toString().length>1)

{

   alert(GetQueryString("url"));

}

So as not being given!

Method two: the traditional method

<Script of the type = "text / JavaScript">
function UrlSearch () 
{
   var name, value; 
   var str = location.href; // get the entire address bar
   ( "?") NUM = str.indexOf var 
   str = str.substr ( num + 1); // obtain all the parameters    of stringvar .substr ( Start  [,  length  ]

   var arr=str.split("&"); //各个参数放到数组里
   for(var i=0;i < arr.length;i++){ 
    num=arr[i].indexOf("="); 
    if(num>0){ 
     name=arr[i].substring(0,num);
     value=arr[i].substr(num+1);
     this[name]=value;
     } 
    } 

var Request=new UrlSearch(); //实例化
alert(Request.id);
</script>

For example, this code is saved as 1.html

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

This time he took to the value of the test


在html里调用
<script type="text/javascript">
var a="http://baidu.com";
</script>
</head>
<body>
<a id="a1" href="">sadfsdfas</a>
<script>
var a1=document.getElementById("a1");
a1.href=a;
</script>

<Script type = "text / JavaScript"> 
var A = "http://xxx.com/gg.htm?cctv"; 
var a.indexOf S = ( "?"); 
var a.substring T = (S + 1);? // t is behind things 

</ script>

stringvar.substr(start [, length ]

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

stringvar

required. A string literal or String object you want to extract the substring.

start

required. Substring starting position required. The index of the first character in the string is 0.

length

Optional. In return the number of characters in the substring to be included.

If the length is zero or negative, it returns an empty string. If this parameter is not specified, the string continues to the end of stringvar.

Here are some relevant parameters:

str.toLowerCase () is converted into lowercase  
str.toUpperCase () String all converted to uppercase

That URL: Uniform Resource Locator (Uniform Resource Locator, URL) 
the full URL consists of these parts:
scheme: // Host: Port / path # Query the fragment? 
Scheme: communication protocol
commonly used http, ftp, maito etc.

host: host
server (computer) domain name system (DNS) host name or IP address.

port: Port number
integer, alternatively, be omitted when using the default port scheme, such as http default port 80.

path: the path
consists of zero or more "/" symbol string spaced, generally used to indicate a file or directory on the host address.

query: Query
Alternatively, for a dynamic web page (e.g., using technology to produce CGI, ISAPI, PHP / JSP / ASP / ASP.NET pages, etc.) to pass parameters, there may be multiple parameters, with the "&" symbol spaced, name and value of each parameter is separated by "=" symbol.

fragment: pieces of information
string that specifies the network resource in pieces. A plurality of pages, for example, noun, targeting fragment can be used directly to a noun. (Also known as the anchor.)

For such a URL

http://www.maidq.com/index.html?ver=1.0&id=6#imhere

We can get the various parts which use JavaScript
1, window.location.href
entire URl string (in the browser address bar is full)
This example returns the value:  http://www.maidq.com/index.html? ver = 1.0 & id = 6 # imhere

2, window.location.protocol
protocol portion of the URL
to the present embodiment Return Value: http:

3, window.location.host
the host portion of URL
according to the present embodiment Return Value: www.maidq.com

4, window.location.port
the URL of part of the port
if the default port 80 (update: Even added: 80), the return value is not the default of 80 characters but empty
in this case the return value: ""

5, window.location.pathname
path portion of the URL (that is, the file address)
of the present Example Return Value: /fisker/post/0703/window.location.html

6, window.location.search
query (parameters) section
in addition to the dynamic language assignment outside, we can also give a static page and use javascript to get the parameter value should believe
in this case the return value:? Ver = 1.0 & id = 6

7, window.location.hash
anchor
according to the present embodiment Return Value: #imhere

Published an original article · won praise 0 · Views 4165

Guess you like

Origin blog.csdn.net/b_just/article/details/103204188