简体   繁体   English

Firefox链接到javascript函数在不打算时打开一个新窗口

[英]Firefox link to javascript function opens a new window when not intended

I have this problem where when I have this html in firefox it opens a new window 我有这个问题,当我在Firefox中有这个html它打开一个新窗口

<a style="float:right;" 
href='javascript:window.location.href="#";'onClick="javascript:addNewRecord();">
New Record</a>

I have tried self.location, window.location, #body, and #h1 as the href. 我尝试过self.location,window.location,#body和#h1作为href。

Originally I had the code as, but in firefox that did not do anything but open a fresh window, and not perform my function. 最初我有代码,但在firefox中没有做任何事情,只是打开一个新窗口,而不是执行我的功能。 The code works perfect in chrome. 该代码在chrome中非常完美。

<a style="float:right;" href="javascript:addNewRecord();">New Record</a>

The canonical inline way is 规范的内联方式是

<a style="float:right;" href="#"
onClick="addNewRecord(); return false">New Record</a>

or better: 或更好:

<a style="float:right;" href="#"
onClick="return addNewRecord()">New Record</a>

where addNewRecord returns false at the end of the function 其中addNewRecord在函数末尾返回false


An even better way is 更好的方法是

window.onload=function() {
  document.getElementById("addLink").onclick=addNewRecord;
}
function addNewRecord() {
  ...
  return false;
}

plus

<style>
#addLink { float:right }
</style>

and

<a href="#" id="addLink">New Record</a>

Since abusing the HREF on a link going nowhere just to get a pointer is frowned upon, you may consider a <span> with an onclick and a cursor:pointer. 由于在链接上滥用HREF只是为了得到一个指针是不受欢迎的,你可以考虑带有onclick和cursor:指针的<span> It does need more effort to make such an element accessible to for example screen readers. 它需要更多努力才能使例如屏幕阅读器可以访问这样的元素。

尝试:

onClick="addNewRecord();return false"

How your code behaves depends entirely on what the addNewRecord() function does (including what it returns). 代码的行为方式完全取决于addNewRecord()函数的作用(包括它返回的内容)。

Without seeing inside that function it's hard to tell, but I'd say that what is happening is inside there. 没有看到内部的功能很难说,但我会说发生的事情就在那里。

Note that what you put in the href="" part probably is not affecting the behaviour you're seeing. 请注意,您在href =“”部分中放置的内容可能不会影响您所看到的行为。

尝试这个

<a onclick="javascript:addNewRecord();">New Record</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 链接仍然使用preventDefault()函数打开新窗口 - Link still opens new window with preventDefault() function 调用Javascript函数的Href链接会打开一个新选项卡,但在Firefox中不起作用 - Href link that calls Javascript function opens a new tab and doesn't work in Firefox 尝试将在新窗口中打开链接的JavaScript移动到可重用的类 - Trying to move javascript that opens link in new window to a reusable class 当链接在新选项卡中打开时如何保留在当前窗口选项卡上 - How to stay on current window tab when the link opens in new tab Javascript window.open()函数打开没有弹出窗口阻止程序的链接 - Javascript window.open() function opens link without popup blocker 无效链接:链接会在新窗口中打开,但不会在页面上显示 - Dead Link: Link opens in new window, but not on the page 模态javascript:提交表单时会打开新窗口 - Modal javascript: new Window opens when submit form 在Firefox重新启动加载项中,如何在打开新窗口(监听窗口打开)时运行代码? - In a Firefox restartless add-on, how do I run code when a new window opens (listen for window open)? 链接到在新窗口中打开结果的POST数据? - Link to POST data that opens the result in a new window? Firefox打开一个新标签页,而不是JavaScript代码 - Firefox opens a new tab instead of javascript code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM