Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

javascript:void(0); Usage and frequently asked question resolution


May 06, 2021 JavaScript



Friends who have used ajax often see code like: Here, the void in here is an operator that specifies that an expression is evaluated but does not return a value. j avascript:void(0) In some cases there will be browser incompatible bugs. L et's take a look at the basics and usage of javascript:void(0), and then let's look at what problems will arise with it and how to solve it.

Tip: Before you learn about the content, you can learn or review what javascript:void(0) is through the javascript:void(0) meaning section!


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

Expression is an expression of the Javascript standard to evaluate. T he parenthesis on the outside of the expression is selected, but it is a good habit to write it. ( Implementation version Navigator 3.0)

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

The following code creates a hyperlink that won't happen to the user in the future. V oid(0) is calculated as 0 when the user links, but has no effect on Javascript.

<A HREF="javascript:void(0)">单此处什么也不会发生</A> 

The following code creates a hyperlink that submits the form when the user is singled out.


<A HREF="javascript:void(document.form.submit())"> 
单此处提交表单</A>

The following code executes the subgo() function,


<a href="javascript:void(0)"onclick="subgo()">点我</a>

Here, javascript:void(0), which does not enlighten the essential role, is just a dead link, executing the function is subgo().


<a href="#" onclick="subgo()">点我</a><ahref="javascript:void(0)" onclick="subgo()">点我</a>区别。
<a href="javascript:void(0)"onclick="javascript:history.back();">返回 </a>

The href" contains a location information. T he default anchor is the top of the page, which causes the browser to slow down or even crash when this link is clicked quickly in a row. J avascript:void(0) represents only a dead link and has no information. So it's best to use void (0) when calling scripts.


The difference between a href and javascript: void (0).
The href" method actually means empty connection, but after clicking it automatically jumps to the top of the page, because using this method is equivalent to clicking on 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 example above, you can use void(0) when you want to do some processing, but not refresh the page as a whole, but be careful if you need to refresh the page.

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

To be clear, the form of href""" So if it's an empty connection, javascript:void(0) is recommended.


When the page has a scroll bar, the click returns to the solution at the top of the page
There are currently several solutions:

1, click on the link and don't do anything
<a href="javascript:void(0);" >test</a> 
<a href="javascript:;" >test</a> 
<a href="####" >test</a> //使用2个到4个#,见的大多是"####",也有使用"#all"等其他的 

2, click on the link, in response to user-defined click events
<a href="javascript:void(0)" onclick="doSomething()">test</a> 
<a href="#" onclick="doSomething();return false;">什么问题都解决了,包括浏览器不兼容问题</a> //或者直接使用href="" 
<a href="#" onclick="alert();event.returnValue=false;">test</a> 

What are the problems with javascript:void (0)?
Linking (href) directly using javascript:void(0) can cause problems in IE, such as causing gif animations to stop playing, etc., so the safest way to do this is to use . T o prevent clicking on the link and jumping to the top of the page, the onclick event return false is sufficient. If you just want to move the mouse over and become a hand, you can use it.

We can use thevoid operator to specify hyperlinks, such as javascript: void (document.form.submit().). T he expression is evaluated but does not load anything at the current document, void(0) is evaluated as 0, but has no effect on JavaScript, that is, the effect is the same.

Since it is easy to cause problems, why Sina Weibo, Taobao and other big stations home JS operation of the href are javascript: void (0); What about it?

javascript:void(0); Usage and frequently asked question resolution
javascript:void(0); Usage and frequently asked question resolution

<a href=”void(0);” rel=”nofollow”>
Click here to do nothing
</a>

<a href=”void(document.body.style.backgroundColor=’green’);” rel=”nofollow”> Click here for green background </a>

It could be javascript: void (0); Both to ensure that the return value is undefined, but also to ensure that if the connection click needs to process some code, at any time to replace 0 on it.

The addition of js to the href is to prevent connection jumps, which were previously used with the hashtag but jump back to the top of the page in some browsers. T hat's not good, so someone thought of adding onclicks with "return false" but the problem came up again, which would block the binding time, for example, when we used jquery. S o there's the use of href" javascript: void (0); " Writing, this practice started out by some people who write c, because of writing habits. I t was later said that the void function had been removed. T here is a more concise writing, in fact, a in no connection when you can completely remove the href property or use other elements, as long as you add a pointing mouse style on it. It depends on your personal habits.

The method that will jump under IE using the javascript:void(0) method
In general, the A label under IE uses the onclick method, adding javascript:void(0) or javascript:;

The original code is as follows:
<a href="javascript:void(0)" title="关闭" onclick="delbook();">关闭</a>
Or:
<a href="javascript:;" title="关闭" onclick="delbook();">关闭</a>

Both methods may jump.

But you'll find that javascript:void(0), or javascript:, will be executed after the clidk event;


Solution:
<a href="javascript:void(0)" title="关闭" onclick="delbook();return false;">关闭</a>
Or
<a href="javascript:void(0)" target="_self" title="关闭" onclick="delbook();">关闭</a>

Use return false; You can prevent javascript:void(0) from executing.


You can prevent a page _self from jumping to another page because it is an empty function, so no page refresh occurs.


Of course, you can write it directly _self use target to "write".


<a href="javascript:delbook()" target="_self" title="关闭">关闭</a>
Use the above workaround as long as there is a refresh or jump action on the page.


Several ways to jump to JS:
1.

window.open(”url“) 

2. Use custom functions

<script> 
function openWin(tag,obj) 
{ 
obj.target="_blank"; 
obj.href = "Web/Substation/Substation.aspx?stationno="+tag; 
obj.click(); 
} 
</script> 
<a href="javascript:void(0)"onclick="openWin(3,this)">点我</a>

3.
window.location.href='';