Skip to:
Content
Pages
Categories
Search
Top
Bottom

Open Links in new window

  • What can I change to have all links that are posted on the site open in a new window instead of the window that the forum is using? Thanks!

Viewing 24 replies - 26 through 49 (of 49 total)
  • Hello,

    I use for a pretty long time rel=”external” more compliant and this works well.

    The following bit of code is from

    http://www.sitepoint.com/article/standards-compliant-world

    function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
    var anchor = anchors;
    if (anchor.getAttribute("href") &&
    anchor.getAttribute("rel") == "external")
    anchor.target = "_blank";
    }
    }
    window.onload = externalLinks;

    If JS is not activated ? well no external link but the site is prefectly working


    _ck_
    Participant

    @_ck_

    TrishaM, if you are using bb-tweaks.php you can just add the above code to the bottom of it and disable the target_blank filter. But it will work either way.

    lstelie, there is another problem with using javascript as it increases the amount of time before the page is ready. If you are going to throw in rel=”external” you might as well do target. (In fact, you don’t even need rel=”external” as you can scan the domain for each link with javascript on the fly.) But I assure you that target will be supported in browsers ten years from now, maybe even twenty. People who insist on perfect validation need to find more productive things to do with their time, there’s alot of code out there that needs to be written! ;-)

    Throw off the shackles of validation!

    (I need that as a bumper sticker)

    People who insist on perfect validation need to find more productive things to do with their time, there’s alot of code out there that needs to be written! ;-)

    Good point :)


    TrishaM
    Participant

    @trisham

    Thanks _ck_ – I’ll do as suggested…….

    P.S. At cafepress.com you can design your own bumper sticker! I’ve made a few of my own t-shirts that way…..perfect for when you know just what you want but can’t find it anywhere…. :)


    citizenkeith
    Participant

    @citizenkeith

    ck’s last version doesn’t work for me… I’m running 0.9.0.6. All links posted in the forum aren’t actually anchored to anything… though CSS styling works fine.


    _ck_
    Participant

    @_ck_

    My code is still working perfectly for me. Post what you are actually using.

    I can’t seem to get _ck_’s plugin to work, everytime I use it takes away the a href…

    <?php
    /*
    Plugin Name: Target Nofollow External Only
    Description: append target="_blank" and rel="nofollow" only on external links
    Plugin URI:
    Author: _ck_
    Version: 0.0.1
    */

    add_filter('post_text', 'target_nofollow_external_only',999); // unfortunately we have to do this on every page load and not in pre_post

    function target_nofollow_external_only( $text ) {
    $domain="easyloungin-forum.com"; // domain to exclude from target and nofollow
    $text = preg_replace('|<a (.*)rel=['"]nofollow['"](.+)?>|iU','<a >', $text); // strip ALL nofollow
    $text = preg_replace('|<a (?=([^>]+http://))(?!([^>]+'.$domain.'))(.+)>|iU', '<a rel="nofollow" target="_blank">', $text); // add back in when needed
    return $text;
    }
    ?>


    _ck_
    Participant

    @_ck_

    Try this simpler version:

    function bb_target_blank( $text ) {
    $text = preg_replace('|<a (.+?)>|i', '<a $1 target="_blank">', $text);
    return $text;
    }
    add_filter('pre_post', 'bb_target_blank');

    EDIT 22 December 2009. bbPress is stripping the $1 out of the code above. It goes between the ‘a’ and the ‘target’ where you can see two spaces. See here:

    http://pastie.org/753419


    johnpeterson1982
    Member

    @johnpeterson1982

    PHP doesn’t have a open new window function, has to be done through HTML. Easiest way to manually change the links. Open you HTML page in a text editor and hit Ctrl + H to bring up the replace box.

    Text To Replace: href=”http://www.mysite.com/pagetosopeni‚Ķ

    Replace With: target=”_blank” href=”http://www.mysite.com/pagetosopeni‚Ķ

    That’ll update all the links to pagetosopeninnewwindow.php to open in new windows.. save and re-upload to your server.

    regards…

    john


    citizenkeith
    Participant

    @citizenkeith

    @_ck_: My code is still working perfectly for me. Post what you are actually using.

    I’m using this (with my domain name inserted):

    <?php
    /*
    Plugin Name: Target Nofollow External Only
    Description: append target="_blank" and rel="nofollow" only on external links
    Plugin URI:
    Author: _ck_
    Version: 0.0.1
    */

    add_filter('post_text', 'target_nofollow_external_only',999); // unfortunately we have to do this on every page load and not in pre_post

    function target_nofollow_external_only( $text ) {
    $domain="citizenkeith.com"; // domain to exclude from target and nofollow
    $text = preg_replace('|<a (.*)rel=['"]nofollow['"](.+)?>|iU','<a >', $text); // strip ALL nofollow
    $text = preg_replace('|<a (?=([^>]+http://))(?!([^>]+'.$domain.'))(.+)>|iU', '<a rel="nofollow" target="_blank">', $text); // add back in when needed
    return $text;
    }
    ?>

    External links don’t actually work… all the CSS works (rollover changes, etc) but the browser (Firefox and IE for Windows) doesn’t actually recognize a link. I can’t click on it and open it anywhere (same window, new tab, etc).


    chrishajer
    Participant

    @chrishajer

    citizenkeith, can you post a URL where it’s not working properly?


    luminousnerd
    Member

    @luminousnerd

    This is more than just personal preference I’m afraid, it’s an issue of statistics. More often than not, if you don’t open external links with a new window or tab, the visitor will never come back to your site. I’ve seen this statistic prove true on several dozen websites and I’m 100% convinced it’s true.

    Sure, some people get all self-righteous about it, and you may lose them because of that, but in exchange you won’t lose the much, much more numerous amount of people who simply would have forgotten to come back. Sorry, I know it’s slightly annoying on the user end, but from the perspective of someone trying to make money with their site, opening external links in the same windows is extremely foolish.

    Edit, ah crap this one’s old too. My bad


    Alex
    Participant

    @cwdnow

    None of these seem to work for me in the latest version of bbPress. Any ideas on how to get this working?


    bobbykjack
    Member

    @bobbykjack

    @luminousnerd: Can you provide links to the research you’ve carried out? Sounds like it would be very useful.

    I have to say, I’m sceptical about your conclusions. If “opening external links in the same windows [sic] is extremely foolish” for “someone trying to make money with their site”, isn’t it foolish to have those external links in the first place?


    Alex
    Participant

    @cwdnow

    The long _ck_ version half works – it links, removes the nofollow tag, and adds target=”_blank” to internal links. But external links aren’t linked – just the CSS is applied. Could you post a pastie of the long version of your code?


    acoburn4
    Participant

    @acoburn4

    @_ck_, I am using your code to open external links in a new window and add the attribute “nofollow”

    My site also has sub-domains, so I wanted to add a case for that. My reg ex skills are not that great. Do you know how I could do that?

    Code I’m using:

    http://pastie.org/2361268

    Thanks for your help!!


    acoburn4
    Participant

    @acoburn4

    @_ck_, Nevermind my earlier post. It is already working on sub-domains. Sorry for the confusion.

    However, I am having the same problem as @cwdnow. Internal links are working as expected: they open in same window with “nofollow” tag removed.

    The code is broken when it deals with external links, though. It adds the attributes target=”_blank” and rel=”nofollow” but the URL is broken. I haven’t dug too deep yet, but I think the reg ex is too broad (selecting too much).

    Here is the code that I’m using:

    http://pastie.org/2361825

    Here is what it is outputting:

    https://picasaweb.google.com/lh/photo/Px6V4Z7PfKk6Gpv5x_Uh4Q?feat=directlink

    I will post an update if I figure out what it is. In the meantime, do you happen to know what is causing this?


    _ck_
    Participant

    @_ck_

    Don’t really have the time to debug all that but if it’s selecting too much, try replacing this

    (.+)

    with this

    ([^>]+)

    which hopefully will stop it from going past the end of the anchor tag


    acoburn4
    Participant

    @acoburn4

    Hi @_ck_

    Thanks for the help. I tried what you posted, but it’s still not working.

    It fixed the broken URLs, but strips the target=”_blank” and rel=”nofollow” attributes. It looks like the second function (for external links) isn’t firing off.

    Will keep working on it and post an update if I figure it out.

    I really appreciate your help!


    _ck_
    Participant

    @_ck_

    I just realized I am not escaping < and > in that code.

    It most probably should be.

    So anywhere you do not see a slash in front, add one, ie.

    <

    >

    actually to be more clear, try this

    $text = preg_replace('|<a (.*)rel=['"]nofollow['"]([^>]+)?>|iU','<a $1>', $text);	// strip ALL nofollow
    $text = preg_replace('|<a (?=([^>]+http://))(?!([^>]+'.$domain.'))([^>]+)>|iU', '<a $1 rel="nofollow" target="_blank">', $text);

    sorry, don’t have time to debug it more, someone will have to throw it into a regex tester and check it against some same sample links


    acoburn4
    Participant

    @acoburn4

    Still not working :(

    The code is coming through differently in my email, so I posted a link to it making sure I have the syntax right: http://pastie.org/2362857

    Internal links are still ok; external links look like this in Firebug:

    https://picasaweb.google.com/lh/photo/Px6V4Z7PfKk6Gpv5x_Uh4Q?feat=directlink

    i need this too.

    links in new windows an with nofollow

    only own url links in same windows without nofollow.


    PascalFromParis
    Member

    @pascalfromparis

    Hello,

    It’s the same problem for me.

    External links are broken. The href=”externallink.com” disappear after using the code.

    I am not a developer, just a blogger who is looking for solutions, but something seems strange to me In this line :

    $text = preg_replace('|]+http://))(?!([^>]+'.$domain.'))(.+)>|iU', '', $text);

    It seems that all the content of ‘ a ‘ is replaced by ‘ a rel=”nofollow” target=”blank” ‘

    So, the href=”…” disappear


    jzn123
    Participant

    @jzn123

    I am using this version, but it’s not working with bbPress v1.1

    Has someone a clue?

    <?php
    
    /*
    
    Plugin Name: Target Blank
    
    Description:  append target="_blank" to all links
    
    Plugin URI:  http://ckon.wordpress.com/2007/07/12/bbpress-plugin-bb-tweaks/
    
    Author:  _ck_
    
    Version: 0.04
    
    */
    
    //add target=_blank to post links
    
    function bb_target_blank( $text ) {
    
    $text = preg_replace('|<a (.+?)>|i', '<a $1 target="_blank">', $text);
    
    return $text;
    
    }
    
    add_filter('pre_post', 'bb_target_blank');
    
    ?>
Viewing 24 replies - 26 through 49 (of 49 total)

You must be logged in to reply to this topic.