of Super Simple Recommendation for Getting Address Bar Parameters Using js

  • 2021-06-28 10:39:21
  • OfStack

Method 1: Obtain address bar parameters using regular expressions: (highly recommended, 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; 
} 
 
//  Call Method  
alert(GetQueryString(" Parameter Name 1")); 
alert(GetQueryString(" Parameter Name 2")); 
alert(GetQueryString(" Parameter Name 3")); 

Here is an example:

If the address bar URL is: abc.html ?id=123 & url=https://www.ofstack.com

So, but you call it this way: alert (GetQueryString ("url");

A dialog box appears: the content is https://www.ofstack.com

If used: alert (GetQueryString ("id");Then the pop-up content is 123;

Of course, if you don't pass parameters, such as if your address is abc.html followed by no parameters, the result of a forced output call can sometimes be wrong:

So let's add a judgment to determine if the parameter we're requesting is empty. First, assign the value to a variable:


var myurl=GetQueryString("url"); 
if(myurl !=null && myurl.toString().length>1) 
{ 
  alert(GetQueryString("url")); 
} 

That way you won't make a mistake!

Method 2: Traditional method


<script type="text/javascript">
function UrlSearch() 
{
  var name,value; 
  var str=location.href; // Get the entire address bar 
  var num=str.indexOf("?") 
  str=str.substr(num+1); // Get all parameters   stringvar.substr(start [, length ]

  var arr=str.split("&"); // Put each parameter in an array 
  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(); // instantiation 
alert(Request.id);
</script>

Save this code as 1.html, for example

Then I want to visit 1.html ?id=test

This is the time to get the value of test

Called in 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 s=a.indexOf("?"); 
var t=a.substring(s+1);// t Namely ? Something behind  

</script>

stringvar.substr(start [, length ]

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

stringvar

Required option.The string literal or String object from which to extract the substring.

start

Required option.The starting position of the desired substring.The first character in the string has an index of 0.

length

Optional.The number of characters to include in the returned substring.

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

Here are a few related parameters:

str.toLowerCase() to lowercase
str.toUpperCase() strings are all capitalized

URL: Unit 1 Resource Locator (Uniform Resource Locator, URL)

The complete URL consists of these parts:

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

scheme: Communication Protocol

Common http, ftp, maito, etc.

host: Host

Server (computer) Domain Name System (DNS) host name or IP address.

port: Port number

Integer, optional, omitted with the default port of the scheme, such as http with a default port of 80.

path: Path

A string separated by zero or more'/'symbols, typically used to represent a directory or file address on a host.

query:Query

Optionally, it can be used to pass parameters to dynamic web pages (such as those made using technologies such as CGI, ISAPI, PHP/JSP/ASP/ASP.NET), which can have multiple parameters and can be used with " &"Symbol separated, each parameter's name and value are separated by"="symbol.

fragment: Information Snippet

String that specifies a fragment in a network resource.For example, if there are multiple noun explanations in a web page, you can use fragment to locate a noun explanations directly. (Also known as anchor point.)

For such an URL

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

We can get the parts with javascript

1, window.location.href

Entire URl string (full address bar in browser)
The return value of this example: https://www.ofstack.com/index.html?ver=1.0 & id=6#imhere

2,window.location.protocol

Agreement Section of URL
Return value for this example: http:

3,window.location.host

Host section of URL
Return value for this example: www.ofstack.com

4,window.location.port

Port section of URL
If the default port of 80 is used (update: even if: 80 is added), the return value is not the default port of 80 but an empty character
Return value for this example: ""

5,window.location.pathname

The path portion of URL (that is, the file address)
The return value for this example: /fisker/post/0703/window.location.html

6,window.location.search

Query (parameter) section
In addition to assigning values to dynamic languages, we can also assign static pages and use javascript to obtain parameter values that we believe correspond to
The return value of this example: ?ver=1.0 & id=6

7,window.location.hash

Anchor Point
Return value for this example: #imhere


Related articles: