Home > Web Front-end > JS Tutorial > How to use Location object

How to use Location object

不言
Release: 2019-01-29 16:57:52
Original
4103 people have browsed it

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.

How to use 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>
Copy after login
主机名:
<input type="text" size="20" maxlength="256" name="HostnameFld" value="php.cn">
Copy after login
路径:
<input type="text" size="20" maxlength="100" name="PathnameFld" value="/">
Copy after login
<input type="button" name="Go" value="Go" onclick="gotoUrl()">
Copy after login

The running effect is as follows

How to use Location object

This article ends here It’s all over. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use Location object. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template