Home >Web Front-end >JS Tutorial >Detailed explanation of Javascript parsing URL method_javascript skills

Detailed explanation of Javascript parsing URL method_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:29:031711browse

URL: Uniform Resource Locator (URL)

The complete URL consists of these parts:
scheme://host:port/path?query#fragment

scheme = communication protocol (commonly used http, ftp, maito, etc.)
host = host (domain name or IP)
port = port number
path = path

query = query
Optional, used to pass parameters to dynamic web pages (such as web pages made using CGI, ISAPI, PHP/JSP/ASP/ASP.NET and other technologies). There can be multiple parameters, separated by "&" symbols, each parameter The name and value are separated by the "=" symbol.

fragment = information fragment
A string used to specify the fragment in the network resource. For example, if there are multiple noun explanations in a web page, you can use fragment to directly locate a certain noun explanation. (Also called anchor point.)

For such a URL
http://www.master8.net:80/seo/?ver=1.0&id=6#imhere

We can use javascript to get various parts of it
1, window.location.href
The entire URl string (the complete address bar in the browser)

2,window.location.protocol
The protocol part of the URL
The return value in this example: http:

3,window.location.host
The host part of the URL
The return value in this example: www.master8.net

4,window.location.port
The port part of the URL
If the default port 80 is used (update: even if :80 is added), the return value is not the default 80 but the empty character
The return value in this example:""

5,window.location.pathname
The path part of the URL (that is, the file address)
The return value in this example:/seo/

6,window.location.search
Query (parameter) part
In addition to assigning values ​​to dynamic languages, we can also assign values ​​to static pages and use javascript to obtain the corresponding parameter values ​​
The return value in this example:?ver=1.0&id=6

7,window.location.hash
Anchor
Return value in this example: #imhere

8. url parameter value

Method 1: Regular Analysis

Copy code The code is as follows:

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;
}

Method 2: Split into arrays

Copy code The code is as follows:

function GetRequest() {
var url = location.search; //Get the string after the "?" character in the url
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;
}

The method is very simple, but very practical. Here are 2 commonly used methods. If you have different methods, please let us know. This article will continue to be updated. Let’s make progress together

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn