【Browser】Location object properties and method analysis

Attributes

1.hash

Definition: The hash attribute is a readable and writable string, which is the anchor part of the URL (the part starting from #).
Syntax: location.hash
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.hash); 输出结果:#/

2.host

Definition: The host attribute is a readable and writable string that can set or return the host name and port number of the current URL.
Syntax: location.host
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.host);//输出结果:localhost:8080

3.hostname

Definition: The hostname attribute is a readable and writable string that can set or return the hostname of the current URL.
Syntax: location.hostname
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.hostname) ;//输出结果:localhost

4.href

Definition: The href attribute is a readable and writable string that can set or return the full URL of the currently displayed document.
Syntax: location.href
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.href);输出结果:http://localhost:8080/#/

5.pathname

Definition: The pathname attribute is a readable and writable string that can set or return the path part of the current URL.
Syntax: location.pathname
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.pathname);//输出结果:/

6.port

Definition: The property is a readable and writable string that sets or returns the port portion of the current URL. If the port number is 80 (which is the default port number), there is no need to specify it.
Syntax: location.port
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.port);//输出结果:8080

7.protocol

Definition: The protocol attribute is a readable and writable string, which can set or return the protocol of the current URL.
Syntax: location.protocol
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.protocol);//输出结果:http:

8.search

Definition: The search attribute is a readable and writable string that can set or return the query part of the current URL (the part after the question mark ?).
Syntax: location.search
Example:

比如当前页面的链接为:http://localhost:8080/#/
console.log(location.search);//因为上面链接无问号,所以返回值为空

method

1.assign()

Function: The assign() method loads a new document.
Syntax: location.assign(URL)
Example:

//html  点击会跳到百度
<input type="button" value="跳到百度" onclick="test()">

//script
function test(){
	window.location.assign("https://www.baidu.com")
}

2.reload()

Function: used to refresh the current document. Similar to the refresh page button on your browser. If the method's parameter is set to true, it will bypass the cache and re-download the document from the server regardless of the document's last-modified date. This is exactly the same as the effect of holding down the Shift key when the user clicks the browser's refresh button.
grammar:location.reload(forceGet)

parameter type describe
forceGet Boolean optional. If the method's parameter is set to true, it will bypass the cache and re-download the document from the server regardless of the document's last-modified date.

Guess you like

Origin blog.csdn.net/m0_46533551/article/details/129518701