Published in: JavaScript
Script takes links that are not on your domain and opens them in a new window. This code allows for XHTML strict validation and meets Accessibility criteria on opening new windows.
function handleExternalLinks() { // function makes sure that external links open in new window var hostName = window.location.hostname; var links = document.getElementsByTagName("a"); for(var i = 0; i < links.length; i++) { if(links[i].href.indexOf(hostName) == -1) { var curTitle = (links[i].getAttribute("title")) ? links[i].getAttribute("title") + " - ": ""; links[i].setAttribute("target", "_blank"); links[i].setAttribute("title", curTitle + "opens in new window"); } } } handleExternalLinks(); // Call the function
You need to login to post a comment.
