The Location object in How to use Location object can be used to access and change the current URL displayed in the visitor's browser. The Location object itself is a property of the Window object. Let's take a look at the specific usage of the Location object.
The Location object is a property of the window and document objects, so window.location and document.location point to the same object.
Let’s take a look at the properties of the Location object
href: The complete URL of the currently loaded page.
protocol: The protocol currently in use.
host: Server name and port number.
hostname: Indicates the server name, subdomain and domain name of the URL.
port: Indicates the communication port used by the specified server.
pathname: The directory and file name of the URL.
search: Represents a string starting with a question mark that specifies any query information in the HTTP URL.
hash: Represents a string starting with #, specifying the anchor name in the HTTP URL.
Let’s look at the specific examples
The code is as follows
<!DOCTYPE HTML> <html> <head> <title>How to use Location object Location Object</title> <script type="text/javascript"> function gotoUrl() { window.location.href = window.document.loctn.ProtocolFld. options[window.document.loctn.ProtocolFld.selectedIndex]. text + document.loctn.HostnameFld.value + document.loctn. PathnameFld.value } </script> </head> <body> <h3>在以下部分输入URL</h3> <form name="loctn" method="post"> <pre class="brush:php;toolbar:false">协议: <select name="ProtocolFld" size="1"> <option>http://</option> <option>file://</option> <option>javascript:</option> <option>ftp://</option> <option>mailto:</option> </select>
主机名: <input type="text" size="20" maxlength="256" name="HostnameFld" value="php.cn">
路径: <input type="text" size="20" maxlength="100" name="PathnameFld" value="/">
<input type="button" name="Go" value="Go" onclick="gotoUrl()">