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


i dynamically add <a> (link) tag 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 link open in new window (i know it's bad, it's required). tried use "target" attribute, have wrong behavior solution too.

my code works in ie , firefox, return false don't work in safari, chrome , opera. don't work mean link followed after new window opened.

i think might because of google maps v3 environment...

edit: see behavior on actual page:

  1. go http://tinyurl.com/29q6nw6
  2. click on marker on map, balloon show.
  3. click on title in balloon, link should open in new window (work in ie , ff not in safari, chrome , opera.

any welcome!

edit2: problem in function "makeinfowindowcontent" in "gmaps3.js". don't use dom create elements (i use string) because html string must passed google maps (for info window - balloon). in fact same link created @ 2 different places. 1 on left, created dom function (as shown in question) works in browsers , 1 in balloon, created html string, 1 don't work in safari, chrome , opera (the link followed after new window open return false).

edit 3: still no clue why happening... if have idea, let me know!

would preventing default action event work maybe? not sure if gmaps handles links in it's info window in other way, if not, should work.

update: first example didn't work. event wasn't passed when using "inline" onclick assign event. 1 tested , found working.

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); 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -