0

Background

I have a snippet of javascript that sites on customer pages. When this script is executed, it loads necessary assets and scripts and then executes the main code.

One of the script's I'm loading is Twitter's bootstrap JS to create a modal window. It's implemented as a jQuery plugin, and adds '$.modal()' to a jquery object.

Problem

This works on all but one customer site. On this particular site, as I invoke $('#idOfWindow').modal();, the page throws a javascript error Object has no method 'modal'

I've verified that the bootstrap code is invoked, and $.fn.modal is called to setup the plugin, My hunch is that something is clobbering the jQuery object, but I'm not sure how to debug this.

Comments

I'm currently using Chrome's Developer Tools to Debug, and I have verified that the plugin is loaded and executed before it's ever called. The client site is using jQuery 1.7.2. I'm familiar with chrome tools, but I'm not a power user.

5
  • 1
    Have you tried stepping through the JavaScript execution in Firebug or Chrome Developer Tools? Commented Nov 16, 2012 at 18:44
  • I have, is there an easy way to see when window.jQuery gets redefined? Commented Nov 16, 2012 at 18:47
  • My bootstrap script is loaded asynchronously, so I can easily break into it and see that it's set. There are a ton of javascript files loaded externally, and I'd like to not have to manually break into each one. Commented Nov 16, 2012 at 18:49
  • What version of jQuery are you using? Commented Nov 16, 2012 at 18:49
  • The site is using 1.7.2. My widget will not load jQuery if jQuery is already loaded. Commented Nov 16, 2012 at 18:52

3 Answers 3

4

If you are right, that your original jQuery object is being overwritten somewhere, you should be able to restore it with jQuery.noConflict().

Demo: jsfiddle.net/KthZu


Edit: Some debugging tips:

To help in debugging, you can check the jQuery version in the console with this code:

$().jquery

Another potential debugging trick would be to store a reference to jQuery when you create you plugin.

$.fn.myPlugin = function(){};
window.jQueryWithMyPlugin = $;

Then, when debugging, you can plainly see if window.$ has been overwritten:

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

1 Comment

BOO YAH! Okay. Looks like $ is being redefined!
1

Generally for any widget type of code that is to be embedded externally, you'll want to wrap your code:

(function($){
    $('#idOfWindow').modal();
})(jQuery);

1 Comment

That's useful when only $ has been reassigned - usually by some other library. But, in case jQuery has been included twice, both jQuery and $ are reassigned. In that case, to get at the original jQuery object, you need to use jQuery.noConflict() to restore $, and jQuery.noConflict(true) if you also want to restore jQuery. Note that this is a 3rd party plugin - Alan isn't calling the plugin from the same script that created it.
0

I have, in the past had issues where multiple versions of jQuery (or jQuery & prototype) were loaded on the same page. Be sure that the version of jQuery you're adding $.fn.modal to is identical to the version that's being called. $().jquery can be used to access the specific version of jQuery in use.

1 Comment

Is there a way to see what version is called by typing something in the javascript console?

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.