JS view the url information of the current page-Kaiqisan

Hello everyone, have you all eaten? I am Kaiqisan, a shameless boy who is not good at words. This time, let's take a look at how to obtain each part of the URL. Although it is a URL, sometimes, we only need a certain part of the URL (protocol information, domain name, routing, parameters...). If we need to get the entire URL every time, the amount of code will rise sharply.

The core content this time is the location object in the BOM

Now take a random URL as an example to expand the explanation

http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

Get the entire url information
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.href	

--->结果

http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

Get the domain name or host name of the url and its port number
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.host

--->结果

kaiqisanOffcial.com:666

Get the domain name or host name of the url
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.hostname

--->结果

kaiqisanOffcial.com

Get the protocol name of the url
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.protocol

--->结果

http:

Get the port number of the url
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.port

--->结果

666

Get the route name of the url
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.pathname

--->结果

/login

Get the parameter name behind the url
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.search

--->结果

?a=1&b=10

Get the anchor point of the current page
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.hash

--->结果

#mobile

Get the protocol, domain name and port
http://kaiqisanOffcial.com:666/login?a=1&b=10#mobile

window.location.origin

--->结果

http://kaiqisanOffcial.com:666

PS: All the above information is returned as a string, please use it correctly after processing

to sum up

No summary

Guess you like

Origin blog.csdn.net/qq_33933205/article/details/108695101