javascript:; vs javascript:void(0);

安稳与你 提交于 2019-11-30 01:47:54

One runs JavaScript that has no statements, the other runs JavaScript that evaluates the statement 0 and then returns undefined.

Neither should be used.

I agree with David that neither should be used. The javascript pseudo-protocol can put the page into a waiting state in some browsers, which can have unexpected consequences. As one example, I spent hours trying to debug a web app that was crashing IE6 whenever someone clicked a javascript: link soon after the page loaded. It turned out that the page entering the waiting state was conflicting with a Flash movie trying to initialize. I solved the problem by replacing the link with one in this format:

<a href="#" onclick="DoSomething(); return false;">Link</a>

The "return false" prevents the link from actually being followed.

Only that the latter javascript:void(0); is more readable and an accepted convention that says this code does nothing.

It's worth noting that industry standards have come a long way regarding this syntax. You should look into Progressive Enhancement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!