The window object provides us with a location property for getting or setting the URL of the form, and can be used to parse the URL. Because this property returns an object, we also refer to this property as the location object. Next, let’s take a closer look. 1. Location Object1. URL Uniform Resource Locator (URL) is the address of a standard resource on the Internet. Every file on the Internet has a unique URL, which contains information about where the file is located and what the browser should do with it. The general syntax of a URL is:
2. Properties of the location objectWe can use these properties to get the corresponding information in the address bar, for example: For example : On the csdn homepage, open our developer tools -> console, enter location, and many properties and return values of the location object will appear: Or we can directly enter the corresponding attributes in the console to get the corresponding return value. For example, we now make an effect of clicking a button to jump to the page: <body> <button>Jump</button> <div></div> <script> var btn = document.querySelector('button'); var div = document.querySelector('div'); var timer = 5; btn.addEventListener('click',function(){ time() }) var time = setInterval(function(){ if(timer == 0) { this.location.href = 'https://www.baidu.com' } else{ div.innerHTML = 'The page will jump after '+timer+' seconds' timer--; } },1000); </script> </body> The running results are: 3. Location object methods
For example, we can also jump to the page by using the location object method: <button>Click to jump</button> <script> var btn = document.querySelector('button'); btn.addEventListener('click',function(){ location.assign('https://www.baidu.com') }) </script> The jump achieved by 2. Navigator ObjectThe navigator object contains information about the browser. It has many properties, the most commonly used of which is userAgent, which returns the value of the user-agent header sent by the client to the server. if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) { window.location.href = ""; //mobile phone} else { window.location.href = ""; //computer} 3. History Object
For example, if we have two pages and want to use one button to go forward and backward, we can bind the forward method and history method to the buttons of the two pages respectively, as shown below: <body> <a href="list.html">Go to the list page</a> <button>Forward</button> <script> var btn = document.querySelector('button'); btn.addEventListener('click',function(){ history.forward() }) </script> </body>
<body> <a href="index.html">Return to the main page</a> <button>Back</button> <script> var btn = document.querySelector('button'); btn.addEventListener('click',function(){ history.back() }) </script> </body> The effect is: Or we can use SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: A small piece of HTML code will include the Baidu search bar in your page
>>: Detailed explanation of the problems and solutions caused by floating elements
1. Mental Journey When I was writing the cockpit ...
Preface The database deadlocks I encountered befo...
Table of contents 1. What I am going to talk abou...
Preface A character set is a set of symbols and e...
WebService Remote Debugging In .NET, the remote d...
Table of contents Preface LED Trigger Start explo...
Preface As one of the best web servers in the wor...
Today's screen resolutions range from as smal...
Copy code The code is as follows: <div id=&quo...
Table of contents Placeholder replacement Console...
background As the business develops, the company&...
This article shares the specific code of JavaScri...
Docker container connection 1. Network port mappi...
1. First, create a hello-world.cpp file The progr...
Use "onInput(event)" to detect whether ...