Home  >  Article  >  Web Front-end  >  How to use Location object

How to use Location object

不言
不言Original
2019-01-29 16:57:523941browse

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>
主机名:
<input>
路径:
<input>
<input>

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!

Statement:
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