"return false" is ignored in some browsers for dynamically added links in the DOM with JavaScript
I am dynamically adding <a> (link) 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 this is bad, but it is necessary). I also tried using the "target" attribute, but I also have the wrong behavior with this solution.
My code works well in IE and Firefox, but returning false does not work in Safari, Chrome, and Opera. It doesn’t work, I mean that after the next opening the link opens.
I think that I may be due to the environment of Google Maps V3 ...
Edit: To see the behavior on a real page:
- Go to http://tinyurl.com/29q6nw6
- Click on any marker on the map, a balloon will appear.
- , ( IE FF, Safari, Chrome Opera.
!
Edit2: "MakeInfoWindowContent", "gmaps3.js". DOM ( ), HTML Google ( - ). . , DOM ( ), , HTML-, Safari, Chrome Opera ( , false).
3: - , ... - , !
? , gmaps - , , .
: . "inline" onclick . , .
function bindEvent(target, event, handler) {
if (typeof target.addEventListener != 'undefined') {
target.addEventListener(event, handler, false);
} else if (typeof target.attachEvent != 'undefined') {
target.attachEvent('on' + event, handler);
}
}
var link = document.createElement('a');
link.href = 'http://www.google.com/';
link.appendChild(document.createTextNode('Google'));
bindEvent(link, 'click', function (e) {
if (typeof e.preventDefault != 'undefined') {
// Common/W3C events
e.preventDefault();
}
// (Old) IE specific events
e.returnValue = false;
var target = e.target || e.srcElement;
window.open(target.href);
return false;
});
document.getElementsByTagName('body')[0].appendChild(link);
@AlexV - inline onclick . : window.open() return false :
1
.
result += ' <h3><a href="' + url + '" onclick="(function(){window.open(' + url + '); return false;})()">' + instituteName + '</a></h3>' + "\n";
:
function openCustomWindow(url) {
window.open(url);
return false;
}
... :
result += ' <h3><a href="' + url + '" onclick="openCustomWindow(' + url + ')">' + instituteName + '</a></h3>' + "\n";
[EDIT] . . - ( return false, ). , this onclick. onclick, this window, . window.href, window.open(undefined) . , <a> . , URL- ( <a href> window.open() .
jQuery:
function initialize(){
// ... code ...
$("#resultMap").delegate("a.myLink").click(function(){
window.open(this.href, "myWindow");
return false;
});
Search();
// ... more code ...
}
function MakeInfoWindowContent(name, instituteName, description, address, url)
{
var result = '<div class="infoBulle">';
if (url != '')
result += ' <h3><a href="' + url + '" class="myLink">' + instituteName + '</a></h3>';
else
result += ' <h3>' + instituteName + '</h3>';
if (Trim(description) != '')
result += ' <p>' + description + '</p>';
result += ' <p>' + address + '</p>';
result += '</div>';
return result;
}
, !
, , , : jQuery.live( , jQuery, )!
:
:
MakeInfoWindowContent, ( onclick ):
result += '<h3><a class="marker_link" href="' + url + '">' + instituteName + '</a></h3>' + "\n";
:
(, DOM ) :
$('div#resultMap a.marker_link').live('click', function(e) {
window.open(this.href);
e.preventDefault();
});
:)
, click . , Google Chrome.