开发的时候有时需要用link(<a>)来触发一些javascript事件,所以常常可以看到如下的代码:
<a href="javascript:void(0)" xxxxx="doSomething();return false;">Link</a> <a href="javascript:void(0)" xxxxx="doSomething();return false;">Link这是一个曾经被多次讨论过的问题,长期以来,我也一直是这样写的。读了 a href=”javascript:void(0);” _fcksavedurl="”javascript:void(0);”" _fcksavedurl="”javascript:void(0);”" — avoid the void 之后,我认同了作者的意见。下面的写法确实更合理:
<a href="#" xxxxx="doSomething();return false;">Link</a> <a href="#" xxxxx="doSomething();return false;">Link
或者
<script type="javascript"> function doSomething() { //doSomething return false; } </script> <a href="#" xxxxx="return doSomething();">Link</a> <script type="javascript"> function doSomething() { //doSomething return false; } Link以往大家不使用"#"的问题是,这将导致点击链接时页面跳回页面顶部,但通过 return false 语句将使得浏览器忽略链接的默认动作,从而避免了此问题。
youngpup 更有意思,他在How to Create Pop-Up Windows 中言辞激烈的倡导大家永远永远永远不要使用 javascript: 伪协议:
Never, ever, ever use the javascript: pseudo-protocol for anything, ever ever ever ever again. Please. Pretty please.
他的解决方案是:
<a href="http://google.com/" xxxxx="window.open(this.href, 'popupwindow', 'width=400,height=300,scrollbars,resizable'); return false;"> <a href="http://google.com/" xxxxx="window.open(this.href, 'popupwindow', 'width=400,height=300,scrollbars,resizable'); return false;">这样的好处就是可以保存到书签或者收藏夹,可以左键单击,也可以右键使用!form Ajax中国