"return false" is ignored in certain browsers for link added dynamically to the DOM with JavaScript

Posted by AlexV on Stack Overflow See other posts from Stack Overflow or by AlexV
Published on 2010-04-29T19:14:58Z Indexed on 2010/04/29 21:57 UTC
Read the original article Hit count: 451

Filed under:
|
|
|

I dynamically add an <a> (link) tag to the DOM with:

var link = document.createElement('a');
link.href = 'http://www.google.com/';
link.onclick = function () { window.open(this.href); return false; };
link.appendChild(document.createTextNode('Google'));
//someDomNode.appendChild(link);

I want the link to open in a new window (I know it's bad, but it's required) and I don't want to use the target attribute.

My code works well in IE and Firefox, but the return false don't work in Safari, Chrome and Opera. By don't work I mean the link is followed after the new window is opened.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dhtml