Skip to content Skip to sidebar Skip to footer

2 Js Functions With Same Name Conflict

Short Using 2 libraries at same page: jQuery UI and Twitter Bootstrap: jQuery UI very important for me because nearly all UI things built based on it Twitter Bootstrap only for sp

Solution 1:

The current version of bootstrap (v2.2.2) now has a noConflict option. Just insert this some place after your bootstrap and jquery script tags but before any usage of the button() function.

<scripttype='text/javascript'>
    $.fn.bootstrapBtn = $.fn.button.noConflict();
</script>

Alternatively, you can use $(document).ready(handler), but you still have to make sure substitution occurs before button() is called.

Once that line of code is executed, button() will be JqueryUI's button, and bootstrapBtn() will be Bootstrap's version.

Solution 2:

To fix the collision between Bootstrap and jQuery UI functions, rename one of them:

<scripttype="text/javascript"src="bootstrap.min.js"></script><scripttype="text/javascript">
    $.fn.bsbutton = $.fn.button;
</script><scripttype="text/javascript"src="jquery-ui.min.js"></script>

And then you can call each function at will:

<scripttype="text/javascript">
    $(".button-one").button()   // jQueryUI button
    $(".button-two").bsbutton()  // Bootstrap button</script>

You can use this technique for any function you need.

Remember the order include A -> rename A -> include B.

Solution 3:

If you are only using the dropdown plugin of Twitter Bootstrap, you don't need the .button() plugin.

Go to the customize bootstrap page and unselect all jQuery plugins, then choose only the dropdown one. You could also unselect some of the CSS if you want.


If you need both implementations (Twitter Bootstrap and jQuery UI), you could rename all .button to something like .bsButton in the bootstrap-button.js file (or find this section in a non-minified version) - not tested.

Solution 4:

All other answers fixing conflict between jQuery UI button and bootstrap button but you can't use bootstrap data-api for renamed button() function. Only manually using new-named bootstrapBtn() function.

I find a solution:

  • Find and replace all occurences of regex (\$[.\w]+\.)button to \1btn in bootstrap.js file.
  • Or use this sed script:

    #!/bin/bash# Change bootstrap3 $.fn.button() function to $.fn.btn() for no conflict with jQuery UI
    sed "s/\(\$[[:alnum:]._]\+\.\)button/\1btn/g" bootstrap/js/bootstrap.js > bootstrap/js/bootstrap.btn.js
    sed "s/\(fn\.\)button/\1btn/g" bootstrap/js/bootstrap.min.js | sed "s/button\((\)/btn\1/g" > bootstrap/js/bootstrap.btn.min.js
    

After that you can use bootstrap data-api for button binding and also manually use btn() function in place of button() for Bootstrap while still use jQuery UI button() function.

Solution 5:

I suggest to do custom jQueryUI, or Bootstrap build - both offer this possibility on websites.

I use custom build of jQueryUI with core, effects, interactions, but without widgets, which often causes conflicts.

Post a Comment for "2 Js Functions With Same Name Conflict"