0
votes

I have some jQuery code that works quite well for opening external links in new windows, although I am running into compatibility issues with my Joomla site and jQuery. I've run into this issue in the past, and the easiest method to resolve it is to use the Joomla system Mootools library.

Here's the jQuery script I need converted into Mootools:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>         
<script type="text/javascript">                                         
   $(document).ready(function() {
      $('a[href^="http://"]').filter(function() {return this.hostname && this.hostname !== location.hostname;}).attr('target', '_blank');
   });                     
</script>

Could someone please help me convert the above code?

Once again, Mootools is already enabled via a system plugin.

3
You could just use better namespacing. - Yahel
check out noconflict in the jquery api. The best way to declare the document.ready event in jQuery is like this: jQuery(function($){...your code here...}); as it will alias jQuery to $ within the context of the function. - zzzzBov
So this code adds target="_blank" to all anchor tags whose href attribute contains a URL that includes a different hostname than the one the page is currently being served from? or tl;dr: makes external sites open in a new window. Right? - matt lohkamp
very possible duplicate stackoverflow.com/questions/4970912/… yep she uses joomla too - KJYe.Name 葉家仁

3 Answers

3
votes

If you might want to select external links again you could set up a new pseudo selector

Slick.definePseudo('external', function() {
    return this.hostname && this.hostname != window.location.hostname;
});

document.getElements('a[href^=http://]:external').set('target', '_blank');

Or exactly as jQuery did it.

document.getElements('a[href^=http://]').filter(function(a) {
    return a.hostname && a.hostname != window.location.hostname
}).set('target', '_blank');
0
votes

I'm not real familiar with mootools, but have you tried something like this?

Snipplr.com: automatically send external links to new window with mootools

... really you don't even need mootools, you can do it in vanilla javascript if you're concerned about conflicts.

0
votes
$('a').each(function(el){ 
    /* check el.href and if test is true => */ 
    el.set('target','_blank')}
);