Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use location object

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use location object". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use location object.

Special window

It is common to mention window on web pages, such as this:

_ window.onload=function () {/ / execute function body}

This code means what to do when the content of the web page is loaded.

In the world of js, window objects play a dual role, both as an interface for accessing browser windows and as Global objects.

Because of this, all variables and functions declared in the global scope become properties and methods of the window object.

Like this:

Var age = 29 function sayAge () {alert (this.age);} alert (window.age); / / 29sayAge (); / / 29window.sayAge (); / / 29

However, the two are not exactly the same. Global variables cannot be deleted through delete, while properties defined directly on window can.

The legacy of history

Early web pages will use more windows and frames, displaying nested web pages in the same window, as well as various pop-up windows such as alert, confirm, prompt, etc., to input or confirm information to users, but with the development of web development technology and design, they have been difficult to trace, so I don't focus on it here.

Location object

Basic attribute

Location is one of the most useful BOM objects, providing information and navigation for documents loaded in the current window. Its main uses are related to url:

● hash: returns the character after the # symbol in url, or null if not.

● host/hostname: returns the server name or port number and so on.

● href: returns the full url

● port: returns the specified port number

● protocol: returns the protocol used

● search: returns the query string of url, that is, everything from the question mark to the end

Position operation

Location can change the location of the browser in a variety of ways, the most common of which is the assign () method, such as:

Location.assign ("http://www.baidu.com");"

This will immediately open a new url and add a record to the browsing history. The following two lines of code are equivalent:

_ window.location = "http://www.baidu.com";location.href =" http://www.baidu.com";

The most common is location.href.

Of course, modifying other properties can also change the currently loaded page. After modifying url in either way, a new record will be generated, and users can navigate to the previous page by clicking the back button, but sometimes we don't want this to happen. We can use the replace () method. Like this:

Location.replace ("http://www.baidu.com");"

It receives only one parameter, the url you navigated to, and does not generate a record, and the user cannot return to the previous page.

Another location-related method is reload (), which literally reloads the current page, but there is a little emphasis here: if it is only reload, without parameters, the page will be reloaded from the browser cache, and if it is forced to reload from the server, you need to pass parameters, like this:

Location.reload (true)

History object

History keeps records of users surfing the Internet, and each browser window, tab, has its own history object associated with a specific window object. For security reasons, developers generally can not know which web pages users have visited, but there is still a way to achieve forward and backward function, the method is go (). For example:

History.go (- 1); history.go (1)

The argument is not only a number, but also a string, and the browser jumps to the first position in the history that contains the string, either forward or backward. such as

History.go ("baidu.com")

In addition, there are more straightforward methods such as back () and forward () for moving forward or backward.

In addition, history also has a length property that stores the number of history records, which you can use if you want to determine if the user opened your page in the first place.

If (history.length = = 0) {/ / do what you want}

The history object is not particularly commonly used, but in some special-purpose designs, you still have to ask it to solve the problem.

Thank you for your reading, the above is the content of "how to use location objects". After the study of this article, I believe you have a deeper understanding of how to use location objects, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report