开发者

jquery forms plugin noConflict problem

开发者 https://www.devze.com 2023-01-06 22:59 出处:网络
I am writing a bookmarklet in which I want to use the jquery forms plugin. The problem I am having is that a web page on which the bookmarklet might be used could be using a different js lib, which a

I am writing a bookmarklet in which I want to use the jquery forms plugin.

The problem I am having is that a web page on which the bookmarklet might be used could be using a different js lib, which already uses '$'. No problem, I can tell jQuery to use noConflict()

However, the forms plugin of jQuery also uses '$' which I have no control over. Is there a way for me to specify to this plugin to not use $ an开发者_开发技巧d use jQuery instead?

More details..

As TNi suggests, I might be misunderstanding the problem.

Here is what I do

I am using Safari 5. Here is what I do (jquery already loaded) ....

var scriptElem = docEl.createElement('script');
scriptElem.setAttribute('src',"http://localhost:81/p/a2b/jquery.form.js");
scriptElem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(scriptElem);

and then

jQuery(docEl).ready(function() {
    jQuery('#a2b_cart').ajaxForm(function () { alert (" yo");});

});

What I see on the javascript console:

jQuery("#a2b_cart").ajaxForm is not a function


Chances are, your code initializing the form is called immediately after adding the script element, but before it has loaded. Since jQuery is already loaded, call the forms file via getScript:

jQuery.getScript("http://localhost:81/p/a2b/jquery.form.js", function () {
    jQuery('#a2b_cart').ajaxForm(function () { alert("yo"); });

    // Rest of your code here, or call out to another function
});

// Don't place code here, form.js hasn't loaded yet.

The callback function will only be run once the script file has loaded.


The Form Plugin uses a technique that allows it to use $ locally within its own code without conflicting with any calls to jQuery.noConflict().

When you are setting up your form to be handled by the plugin, just use jQuery in place of $. If the problem still persists, we will need to see specific code in order to help.

0

精彩评论

暂无评论...
验证码 换一张
取 消