XHTML: Open a link in a new window - V2
Marc-Antoine Ross February 3rd, 2007
I used my friend Mario’s great post on how-to open a link in a new window to create a script that will open any external link in a new window without the need to add a CSS class to it. This is very useful when you want to change the behavior on a blog that already has a lot of posts.
<script type=”text/javascript” language=”JavaScript”>
function externalLinks() {
if (!document.getElementsByTagName) {
return;
}
var anchors = document.getElementsByTagName(”a”);
var sDom = document.domain;
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute(”href”)) {
iPosition = anchor.href.indexOf(sDom);
if (iPosition == -1 ||
iPosition > sDom.length) {
anchor.target = ”_blank”;
}
}
}
}
window.onload = externalLinks;
</script>
Entry Filed under: JavaScript, XHTML


Leave a Comment
Some HTML allowed
Trackback this post | Subscribe to the comments via RSS Feed