JavaScript Window Location

JavaScript Window Location

The window.location object is used to obtain the address (URL) of the current page and redirect the browser to the new page.

Window Location

window.location object can be written without using the window prefix. Some examples:

Some examples:

location.hostname Returns the domain name of the web host

location.pathname Returns the path and file name of the current page

location .port Returns the port of the web host (80 or 443)

location.protocol Returns the web protocol used (http:// or https://)

Window Location Href

location.href property returns the URL of the current page.

Example

Return the entire URL (of the current page):

<script>
document.write(location.href);
</script>

The output of the above code is:

http://php.cn/js/js-window-location.html

Window Location Pathname

The location.pathname property returns the pathname of the URL.

Example

Return the path name of the current URL:

<script>
document.write(location.pathname);
</script>

The output of the above code is:

/js/js-window-location.html

Window Location Assign

location. The assign() method loads a new document.


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <head> <script> function newDoc(){ window.location.assign("http://www.ask.php.cn") } </script> </head> <body> <input type="button" value="加载新文档" onclick="newDoc()"> </body> </html>
submitReset Code