HTML Hyper Link in New Windows by XHTML 1.0 standard


We all know that we can have tag target=”_blank” to explicitly open a document in new window:

<a href="document.html" target="_blank">Open in New Window</a>

This is OK in HTML4.01 but not allowed in XHTML 1.0, in which you can use attribute rel=”external” instead.

<a href="document.html" rel="external">Open in New Window</a>

And this complies to the XHTML strict scheme, so put a HTML document declaration at the beginning:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

You can also use the following Javascript code to make this work in browsers that does not support XHTML (unlikely, e.g. old versions of IE) or in case the HTML declaration header is missing.

1
2
3
4
5
6
7
8
9
10
11
// helloacm.com
function externallinks() { 
    if (!document.getElementsByTagName) return; 
    var anchors = document.getElementsByTagName("a"); 
    for (var i = 0; i < anchors.length; i++) { 
        var anchor = anchors[i]; 
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
            anchor.target = "_blank"; 
    } 
} 
window.onload = externallinks; 
// helloacm.com
function externallinks() { 
    if (!document.getElementsByTagName) return; 
    var anchors = document.getElementsByTagName("a"); 
    for (var i = 0; i < anchors.length; i++) { 
        var anchor = anchors[i]; 
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
            anchor.target = "_blank"; 
    } 
} 
window.onload = externallinks; 

Alternatively, save the Javascript function in a file external.js and import easily in every HTML file you need it (better to placed between head tag)

<script async type="text/javascript" src="external.js"></script> 

The tag async tells the browser to load the Javascript asynchronously so that it does not block the rendering of the page, which will not slow down the page loading.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
320 words
Last Post: Learning Powershell - Recursive Fibonacci Computation
Next Post: Batch Check Machines Up/Down in IP ranges using Powershell

The Permanent URL is: HTML Hyper Link in New Windows by XHTML 1.0 standard

Leave a Reply