XHTML Targetted Hyperlinks


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //for links in XHTML that should open in another window (since the target attribute is invalid)
  2. //to use it, add class names to the links like so: target-windowName, target-_top, or simply target- for "_blank"
  3. //BEWARE! it replaces any function that is already assigned to the onclick event
  4. function setUpTargetLinks()
  5. {
  6. var links = document.getElementsByTagName("a");
  7. var name;
  8. for(a in links)
  9. {
  10. name = a.className.replace(/(?:^|.*\s)target-([a-z0-9_-]*).*$/i, "$1");
  11. if(name == "_self") continue;
  12. if(name == "_blank") name = "";
  13. if(name == "_top")
  14. a.onclick = function(){ window.top.location = a.href; return false; };
  15. else if(name == "_parent")
  16. a.onclick = function(){ window.parent.location = a.href; return false; };
  17. else
  18. a.onclick = function(){ return !window.open(a.href, name); };
  19. }
  20. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.