Firefox reference to javascript function opens a new window if it is not intended

I have this problem, when I have this html in firefox, it opens a new window

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

I tried self.location, window.location, #body and # h1 as href.

Initially, I had code like, but in firefox, it did nothing but open a new window and not execute my function. The code works fine in chrome.

 <a style="float:right;" href="javascript:addNewRecord();">New Record</a> 
+4
source share
4 answers

How your code behaves depends entirely on what the addNewRecord () function does (including what it returns).

Not seeing this function inside, it's hard to say, but I would say what is happening inside.

Please note that what you put in the href = "" part probably does not affect the behavior you see.

+3
source

Canonical Inline Method

 <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


An even better way is

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

a plus

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

and

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

Since the abuse of HREF over a link going nowhere to get a pointer frowned, you can consider <span> with onclick and the cursor pointer :. More effort is required to make such an element available, for example, for reading from the screen.

+10
source

try:

 onClick="addNewRecord();return false" 
+4
source

try it

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

All Articles