WordPress.org

Ready to get started?Download WordPress

Forums

Comment author's links (9 posts)

  1. mickej
    Member
    Posted 8 years ago #

    Hi,

    I wonder where to add target="_blank" to have author's links in the comments to open in a new windows instead as default (the same windows).

    // Micke

  2. niziol
    Member
    Posted 8 years ago #

    Try not to use the target attribute, as it's not valid XHTML.

    Place the following script in somewhere in the head tags:

    function makeNewWindows() {
    if (!document.links) {
    document.links = document.getElementsByTagName('a');
    }
    for (var t=0; t<document.links.length; t++) {
    var extlinks = document.links[t];
    if (extlinks.href.search(/http/) != -1) {
    if (extlinks.href.search('/DOMAIN/') == -1) {
    extlinks.setAttribute('target', '_blank');
    }
    }
    }
    }
    window.onload = function(e) {makeNewWindows();}

    Replace DOMAIN with whatever the domain name for your site is, this should open all external links in a new window - note: ALL external links!

    Cheers,
    Michael.

  3. mickej
    Member
    Posted 8 years ago #

    "target" is not valid XHTML, r u kidding me? So this is the easiest way to do it? omg

    Well, it works fine.

    Thx!

    Edit: I tried to validate, and the validator does not complain about the target="_blank" tags...

  4. niziol
    Member
    Posted 8 years ago #

    It is deprecated in HTML, but it will still work, if you validate your document at XHTML 1.0 Strict or XHTML 1.1 it will fail.

    They will work in most browsers, so you can do as you wish.

  5. mickej
    Member
    Posted 8 years ago #

    Well, your script worked fine! Thx very much!

  6. b3achy
    Member
    Posted 8 years ago #

    On what page should that code be placed? On the comments page or the main wordpress page?

  7. moshu
    Member
    Posted 8 years ago #

    Place the following script in somewhere in the head tags:

    Which means usually the header.php file of your theme.

  8. b3achy
    Member
    Posted 8 years ago #

    Okay, thanks. I'm learning one day at a time :)

  9. mfrklic
    Member
    Posted 8 years ago #

    I tried the JavaScript code in my header.php and it works great! But I can not get it to validate...
    InValid XHTML

    Placed right after the </style>

    <script type="text/javascript">
    function makeNewWindows() {
    if (!document.links) {
    document.links = document.getElementsByTagName('a');
    }
    for (var t=0; t<document.links.length; t++) {
    var extlinks = document.links[t];
    if (extlinks.href.search(/http/) != -1) {
    if (extlinks.href.search('bluetoothwirelesslife.com') == -1) {
    extlinks.setAttribute('target', '_blank');
    }
    }
    }
    }
    window.onload = function(e) {makeNewWindows();}
    </script>

    Any ideas on how to validate the javascript?

Topic Closed

This topic has been closed to new replies.

About this Topic