Return to Snippet

Revision: 3773
at September 13, 2007 10:26 by skatan


Updated Code
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

Revision: 3772
at September 13, 2007 10:25 by skatan


Updated Code
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

Revision: 3771
at September 13, 2007 10:12 by skatan


Initial Code
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

Initial URL

                                

Initial Description
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.

Initial Title
open external links in new window

Initial Tags
javascript, html

Initial Language
JavaScript