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.
<a href="javascript:void(0);" >test</a>
<a href="javascript:;" >test</a>
<a href="####" >test</a> //使用2个到4个#,见的大多是"####",也有使用"#all"等其他的
<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>
<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.
<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;
<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>
window.location.href='';