admin管理员组

文章数量:1423742

Im not sure what i have to put in the '???' to make it check if the website is my address or not. Will this also work for google adsense ads(just wondering, but not important)?

I was thinking of using a logicial operator like 'not'. So it will check if NOT my website? So then the ??? would be my website?

$j(!a[href=???]).click(function(){
                window.open(this.href, "target=_blank");
                return false;
            });

Im not sure what i have to put in the '???' to make it check if the website is my address or not. Will this also work for google adsense ads(just wondering, but not important)?

I was thinking of using a logicial operator like 'not'. So it will check if NOT my website? So then the ??? would be my website?

$j(!a[href=???]).click(function(){
                window.open(this.href, "target=_blank");
                return false;
            });
Share Improve this question edited May 27, 2012 at 23:53 Joel Coehoorn 417k114 gold badges578 silver badges815 bronze badges asked Dec 8, 2011 at 23:22 chasethesunnnchasethesunnn 2,2645 gold badges28 silver badges43 bronze badges 3
  • 1 You realize that this way, you break all your links if a popup blocker is enabled? It's much, much better to do this directly in the HTML source, where a target="_blank" will work without triggering popup blockers. – Pekka Commented Dec 8, 2011 at 23:27
  • @Pekka so i see using the .click() with window.open would break the links, but the method below that uses the .attr() and adds that html code in that you mentioned should not break it, correct? – chasethesunnn Commented Dec 8, 2011 at 23:34
  • 1 the method below with adding target="_blank" shouldn't break it, you're right. – Pekka Commented Dec 8, 2011 at 23:34
Add a ment  | 

4 Answers 4

Reset to default 5

Try this:

$j('a')
    .not('[name^="http://your.domain./"]')
    .attr('target', '_blank');

Update

My previous fix only works if all URLs are absolute, which was a bad assumption. Try this instead:

$j('a[name^="http:"], a[name^="https:"]')
    .not('[name^="http://your.domain./"]')
    .attr('target', '_blank');

This new version skips all relative URLs. If all of your in-site URLs are relative (i.e. don't start with https?:), you can skip the call to .not.

Running anything like $( 'a' ) is going to loop through EVERY A element - you can only worry about it on the actual click. Also, you can just run with relative urls being your site, and absolute url's being someone else's.

$( document ).on( 'click', 'a', function( event ){
  var $a = $( this );
  // test for anything like `http://` or '//whatever' or 'ftp://'
  if ( /^\w+?\:?\/\//.test( $a.attr( 'href' ) ) ){
    // since this runs before the event is propagated,
    // adding it now will still work
    $a.prop( 'target', '_blank' );
  }
});

Demo: http://jsfiddle/danheberden/3bnk9/

Or you can use window.open:

$( document ).on( 'click', 'a', function( event ){
  var href = $( this ).attr( 'href' );
  // test for anything like `http://` or '//whatever' or 'ftp://'
  if ( /^\w+?\:?\/\//.test( href ) ){
    // dont follow the link here
    event.preventDefault();

    //  open the page
    window.open( href, '_blank' );
  }
});

Demo: http://jsfiddle/danheberden/NcKdh/

You can set up a class to do it:

// Outbound Links
var outLinks = function() { $('a[@class*=out]').click( function(){ this.target = '_blank'; } ); }
$(document).ready(outLinks);

Then all you need do is add the "out" class to any link and it will open a new window.

Or for any link which starts with http://

$('a[href^="http://"]').prop("target", "_blank");

How about:

$j('a').live('click', function(){
  if(this.href.indexOf('yourwebsite.') == -1) {
    window.open(this.href, "target=_blank");
    return false;
  }
});

This could also be improved with a regex so that it doesn't catch URLs like http://someothersite./yourwebsite./, but it's an edge case.

本文标签: javascriptOpen all links not part of my website in new window with jqueryStack Overflow