(link) to the DOM...">

"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:

!

Edit2: "MakeInfoWindowContent", "gmaps3.js". DOM ( ), HTML Google ( - ). . , DOM ( ), , HTML-, Safari, Chrome Opera ( , false).

3: - , ... - , !

+5
11

? , 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);
+3

href 'javascript:void(0);' this.href onclick URL-.

+2

script, javascript return false. . , / JavaScript.

script, , , , .

+1

  • Google - ( ) html , js . gmap.

  • function MakeInfoWindowContent() - , .

  • ( ) : addEventListener .

+1

script, ?

var link = document.createElement('a');
link.href = 'http://www.google.com/';
link.target = '_blank';
link.appendChild(document.createTextNode('Google'));
0
var link = document.createElement('A');
link.setAttribute('href', 'http://www.google.com/');
link.setAttribute('onclick', 'window.open(this.href, "_blank");return false;');
link.appendChild(document.createTextNode('Google'));
0

:

link.onclick = function(event) {
    window.open(this.href);
    if (event.stopPropagation)
        event.stopPropagation();
    return false;
};

DOM Chrome-Developer-Tools, .

0

" DOM". HTML. MakeInfoWindowContent ( gmaps3.js) DOM.

function MakeInfoWindowContent(name, institutionName, description, address, url) {
    var result = document.createElement('div');
    result.className = 'infoBulle';
    // etc.
    return result;
}
0

@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() .

0

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;
}
0

, !

, , , : 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.

0

All Articles