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

What are the usage and common problems of void (0) in javascript

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge about the usage and common problems of void (0) in javascript. Many people will encounter this dilemma in the operation of actual cases. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The format of void operator usage is as follows:\ 1. _ javascript:void (expression)\ 2. _ javascript:void expression

Expression is an expression of the Javascript standard to evaluate. The parentheses on the outside of the expression are selected, but it is a good habit to write them. (implement version Navigator 3.0)

You can use the void operator to specify hyperlinks. The expression is evaluated but nothing is loaded at the current document.

The following code creates a hyperlink that nothing will happen to the user in the future. When a user links, void (0) is calculated as 0, but there is no effect on Javascript.

Nothing will happen here alone.

The following code creates a hyperlink that submits the form when the user orders.

Submit the form here

The following code executes the subgo () function

Click me.

In this case, _ javascript:void (0), which doesn't actually work, is just a dead link, and the function executed is subgo ().

Click me to tell the difference. Return

Href= "#", which contains a location information. The default anchor is # top, the top of the web page, which can cause the browser to slow down or even crash when you click on this link continuously. On the other hand, _ javascript:void (0) only represents a dead link without any information. So it's best to use void (0) when calling the script.

The difference between href= "#" and javascript:void (0)

The href= "#" method actually means empty connection, but it automatically jumps to the top of the page after clicking, because using this method is equivalent to clicking an anchor, but the anchor does not write ID, so it jumps to the top of the page by default. As you can see from the above example, you can use void (0) when you want to perform some processing but do not refresh the page as a whole, but be careful when you need to refresh the page.

In fact, we can use it this way, this sentence will do a submit operation. Then under what circumstances use void (0) more, no refresh, of course, is Ajax, look at the web page of Ajax, you will generally see a lot of void (0), so before using void (0), it is best to think about whether this page needs to be refreshed as a whole.

To put it bluntly, href= "#" refreshes the page as a whole, while href= "_ javascript:void (0)" does not. So if it is an empty connection, _ javascript:void (0) is recommended.

Href = "#" when the page has a scroll bar, the solution that will return to the top of the page after clicking

At present, there are several solutions:

1. Don't do anything after clicking the link

Test test test / / use 2 to 4 #, most of them are "#", but also use "# all" and other

2. After clicking the link, respond to the user-defined click event

Everything has been resolved, including browser incompatibility / / or directly using href= "" to close

Or:

Close

Or

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