Firefox javascript return false in href redirects the browser and displays false

my following code works well in chrome.

<body> <a href="javascript: sam.save();">hehe</a> <script> var sam = { save : function() { alert("here") return false; } } </script> 

when the page redirects and false are displayed on the screen during startup in firefox with the contents of the address bar, as in the figure

enter image description here

firefox version - 9.0.1

suggestions and cheats, please ...

+6
source share
6 answers

The code you quote cannot lead to the behavior you observe. The observed behavior will only happen if sam.save() returns false , while the cited code returns undefined . What does your actual complete code look like?

Edit: The helpful answer was in the comment. I put it here to make it easier to find.

Oh, I missed "return false" after the warning. In this case, the behavior you see is true: if javascript: execute returns a value other than undefined, that value is treated as an HTML string and displayed. At least in most browsers. - Boris Zbarsky on June 11 at 15:25

+4
source

For some reason, return false does not work in FF inside href = "javascript:", but void (0) does.

 <a href="javascript: sam.save();void(0);">hehe</a> 
+6
source
 <a href="#" onclick="sam.save();">hehe</a> 
+5
source

More compatible syntax should be

 <a href="javascript:void(0)" onclick="sam.save()">hehe</a> 
+2
source

try

 `<a href="javascript: void(sam.save())">hehe</a>` 

Hope this helps ...

0
source

An example of how this works:

Firefox: second and third rendering of a white page

Chromium: the first case is a white page

 <body> <a href="javascript: something.with_early_return();">with_early_return</a> <a href="javascript: something.with_false();">with_false</a> <a href="javascript: something.with_string();">with_string</a> <script> var something = { with_early_return: function() { alert("with_early_return"); return; }, with_false: function () { alert('with_false'); return false; }, with_string: function () { alert('with_string'); return 'It renders this as text'; } } </script> 
0
source

Source: https://habr.com/ru/post/917630/


All Articles