Jump to Navigation

Strict HTML/XHTML and target=”_blank”

To keep this short: THERE IS NO target=”_blank”! It’s not valid “Strict HTML” or “Strict XHTML”. There is a huge discussion on the web about this, and frankly I don’t care. I see reasons for opening new windows, so I want new windows and “Strict XHTML”. But how to do that? Easy, I coded a short Javascript which will find all external links and links to PDFs and Ads and makes them open up in new windows.

It finds all links automatically and if you want a specific link to be opened in a new window too, you’ll simply need to add rel="external" as an attribute of the link. Example:

<a href="http://www.junny.de/" rel="external">Junny's Blog</a>

People with Javascript disabled will have a normal link, whereas people with Javascript enabled will have a new window opened for them. You can download the script “Open In New Window” (oinw.js) here.

The script can be included just like any other Javascript:

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

3 Responses to “Strict HTML/XHTML and target=”_blank””

  1. cybercake:

    Good idea. However it is the new-window functions in JS should be banned, not the ‘_blank’, with which a new window is opened only by clicking on the link and this acts also as the user expects.

  2. Frieder:

    Hallo Qian,
    zu diesem Thema gibt es wirklich reichlich Abhandlungen im Netz.
    In meinem letzten Projekt in dem ich “jQuery” eingebunden habe (für Navi etc.) habe ich alle links die auf externe Seiten verweisen mittels einer kleinen Funktion in einer neuen Seite (_blank)geöffnet.
    //*******Funktion öffnet alle links die nicht mit “http://localhost” anfaengt in einem neuen Fenster ***********
    $(“a:not([href^='http://localhost'])”).click(function() {
    window.open($(this).attr(“href”), “newWin”);
    return false;
    }
    );
    ***********************************
    ‘localhost’ muß natürlich ersetzt werden durch URL auf der die Seite gehostet ist. Wie gesagt, funktioniert nur im Zusammenhang mit jQuery.
    Gruß aus Stuttgart

  3. Edde:

    Hi Qian,
    Wonderful solution! Thanks! All _blanks are gone!

    There seems to be an issue with IE7:
    In the footer of all pages I include a “Valid XHTML” link to http://validator.w3.org/check?uri=referer
    Of course this link opens in a new window and validates the page – thanks to you – as XHTML 1.0 Strict.

    Clicking the link in IE7 generates an error: w3.org claims the page can’t be validated because no headers were sent.
    This error occurs only in IE – Firefox, Google Chrome and Safari work fine. Also, IE DOES open the link in a new window, so that part of the script works fine.

    Probably IE is, once again, not sticking to web standards, but I thought you might want to know.

    Thanks again for this nice solution,

    Edde