0

The following jQuery script creates compatibility issues with other java/mootools script on my site - is there anything I can do to make it more compatible?

<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>

Note - the script is designed to open external links in new windows.

2 Answers 2

2

Yes, either replace all $ with jQuery, or use jQuery's noConflict() mode which doesn't assign jQuery to $.

Or you could do this...

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

...because the first argument to that anonymous function is the jQuery namespace. Here I have reassigned it to $ internally.

It should work as expected.

Sign up to request clarification or add additional context in comments.

Comments

0

Elaborate on what you mean by compatibility issues.

It's possible you may need to use jQuery.noConflict(), especially when using jQuery in conjunction with other libraries such as MooTools.

However, since you already have MooTools, why not just use it instead?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.