On 28/11/06, "Jörn Zaefferer" <
Enchos@...> wrote:
> > > New to jQuery, and I'm trying to use it to set my external links to open
> > a
> > > new window without using any attributes to mark the external links. I
> > can
> > > do this with regular js, using a.href.match, but I can't get it to work
> > with
> > > jQuery. Tried various directions, but no go.
> > >
> > > Sure I'll feel pretty stupid when this is answered, but here is my ugly,
> > > non-functioning code:
> > >
> > > $(function(){
> > > if ($!("a").href.match("
http://www.internal.com")) {
> > > $("a").click(
> > > function() {
> > > window.open(this.href); return false;
> > > console.log("external");
> > > };
> > > );
> > > };
> > > });
> >
> > This may work (untested), grabs all anchors that don't begin with http
> > (internal links should be referred to using '../../foo.html' or
> > '/bar.jpg'):
> >
> > $("a").not("[@href^=http]").click(
> > function(){
> > window.open(this.href);
> > console.log('external');
> > return false;
> > }
> > )
> >
> > $("a:not[@href^=http]") may also work.
>
> When testing, keep in mind that browsers behaviour with href attributes is quite inconsistent. While some return only the value as found in the markup, others add the domain and protocol.
> --
So perhaps an additional check is needed, i.e. use location.hostname?
$("a").not("[@href^=http]").click(
function(){
if(this.href.indexOf(location.hostname) == -1) {
window.open(this.href);
console.log('external');
return false;
}
}
)
_______________________________________________
jQuery mailing list
discuss@...
http://jquery.com/discuss/