开发者

Change the JQuery version used in richfaces

开发者 https://www.devze.com 2023-04-11 13:06 出处:网络
I am using richfaces 3_3_3.Final, and I wan\'t to use the latest jQuery http://code.jquery.com/jquery-latest.pack.js, but it seems to have conflicts.

I am using richfaces 3_3_3.Final, and I wan't to use the latest jQuery http://code.jquery.com/jquery-latest.pack.js, but it seems to have conflicts.

Richfaces already load a jQuery version (witch is not the suitable version, seems to be 1.3.2) :

<script type="text/javascript" src="/project/a4j/g/3_3_3.Fi开发者_如何学Cnalorg/richfaces/renderkit/html/scripts/jquery/jquery.js.jsf">

Can I use the latest version for my Javascript process, and allow RichFaces to use its own version, and how?

Is jQuery.noConflict() a good area of research ?


Yes, jQuery.noConflict is a good start, but since the "other library" you are using is also jQuery, there will probably be complications.

When you load jQuery, it defines $ and jQuery as global variables. Using .noConflict will "relinquish control of the $ variable" - meaning whatever $ was before, it will be set to again. If v1.3.2 is loaded first and you load latest pack (1.6.4 as of right now) next, .noConflict will make $ refer to v1.3.2 but jQuery will still refer to 1.6.4.

Basically, you need to alias jQuery before you load the latest pack. See my fiddle used to simulate your situation (also pasted below).

<script type="text/javascript">
    //alias v.1.3.2
    var $132 = $;
</script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
    //alias v1.6.4
    var $164 = $, jQuery164 = $;

    //reset original variables to v1.3.2
    $ = jQuery = $132;

    console.log("v1.3.2: ", $().jquery, jQuery().jquery)
    console.log("v1.6.4: ", $164().jquery, jQuery164().jquery)

    $('div')      //selected using v1.3.2
    $164('div')   //selected using v1.6.4
</script>

After loading 1.6.4, you need to alias it and set $ and jQuery back to the 1.3.2 objects. This doesnt use .noConflict becuase it essentially does the same thing. RichFaces will continue to work using 1.3.2 and you will write your code against the aliased 1.6.4 version. In my fiddle, that means using $164 instead of using $. I hope that makes sense. I am sure the fiddle will be clearer.


Finally, it is fine to develop code using jquery-latest.pack.js but you shouldn't ever use that on your production website. The reason is because at some point in the future, jQuery will be updated. If you are loading the latest pack, those updates may break the functionality of your site without you even knowing it. It's safer to just pick a version and stick to it, only upgrading only when you confirmed that your site will continue to function properly.

0

精彩评论

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

关注公众号