I had a strange error where Prototype and jQuery were overwriting each other’s namespace. Please don’t ask me why I happen to using both - it’s just on one page, and it’s just until I work out how to duplicate my Prototype effort in JQ (I find JavaScript utterly incomprehensible and incredibly difficult to learn).
Anyway, I tried a number of tricks with loading order and including various bits of each. I couldn’t keep it working - either I had one or the other, never both. Then I read this page: Using jQuery with Other Libraries on the official jQuery developer pages. It recommends you do as such:
<script> jQuery.noConflict(); // Use jQuery via jQuery(...) jQuery(document).ready(function(){ jQuery("div").hide(); }); // Use Prototype with $(...), etc. $('someid').style.display = 'none'; script>
But I don’t have any Prototype script in HEAD
. It’s all addressed through the default Rails includes. I didn’t want to start messing with that.
I implemented the above, it didn’t work but the failure was different - now Prototype was trying to work, but erroring out with prototype TypeError: Value undefined (result of expression $) is not object
.
Solution: On a lark, I removed jQuery.noConflict();
and renamed all jquery-targeted $(function)
shortcuts to jQuery(function)
:
<script> jQuery(document).ready(function(){ jQuery("div").hide(); }); script> // in body: Ajax.new & co Rails commands
It’s a horrible nasty hack that gets around the horrible nasty problems from using two JS frameworks on the same page. Don’t copy this, I’m posting it only for your morbid curiosity. If you dare copy this horrible technique your Good Programmer’s Card is revoked. But it works!