/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//for links in XHTML that should open in another window (since the target attribute is invalid) //to use it, add class names to the links like so: target-windowName, target-_top, or simply target- for "_blank" //BEWARE! it replaces any function that is already assigned to the onclick event function setUpTargetLinks() { var links = document.getElementsByTagName("a"); var name; for(a in links) { name = a.className.replace(/(?:^|.*\s)target-([a-z0-9_-]*).*$/i, "$1"); if(name == "_self") continue; if(name == "_blank") name = ""; if(name == "_top") a.onclick = function(){ window.top.location = a.href; return false; }; else if(name == "_parent") a.onclick = function(){ window.parent.location = a.href; return false; }; else a.onclick = function(){ return !window.open(a.href, name); }; } }