jQuery is not defined (in [Symfony] templating environment)

元气小坏坏 提交于 2019-12-05 23:59:35

It is running before the script/DOM is loaded. Put the jQuery.noconflict(); in a jquery wrapper

jQuery(function()
{
  jQuery.noConflict(); 
});

If you DO think you might need to protect yourself:

  (function($)
    {
      $.noConflict(); 
    })(jQuery);

also works

EDIT: Fixed some syntax errors

This is down to JQuery not being loaded in 99% of all cases.

  • Use Firebug's "net" tab to check out whether your JQuery file is actually being loaded.

  • Also, it could be that you're referencing JQuery twice, I think that could cause JQuery to vanish as well.

if that doesn't help, I suggest you dump the <script> includes into your question, the way the turn up in your browser.

What worked for me was to use absolute references versus relative to the scripts although this is a security nightmare for anyone not doing only internal web projects.

jQuery is probably being loaded asynchronously. For example, if you are using the Google Ajax APIs like the following, then you must wrap any code that depends on jQuery in a callback passed to google.setOnLoadCallback():

<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  // Load jQuery (asynchronously)
  google.load("jquery", "1", { uncompressed: true });

  // Won't work -- jQuery is not necessarily loaded yet
  jQuery(function ($) {
    // Document-ready operations
  });

  // Must use the setOnLoadCallback
  google.setOnLoadCallback(function () {
    // jQuery is now loaded
    jQuery(function ($) {
      // Document-ready operations
    });
  });
</script>

Alternatively, you could avoid the asynchronous loading by directly referencing jQuery in a script tag:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

You should call jQuery.noConflict() immediately after including jQuery:

<script src="jquery.js" type="text/javascript"></script>

<script type="text/javascript">
/*<![CDATA[*/
jQuery.noConflict();
/*]]>*/
</script>

<!-- more javascript includes -->

Maybe link to external jQuery is wrong? It seems that code should work. Try to get rid of CDATA blocks - chances are you don't need that.

Wrap your jQuery code (and if you really need it, the call to jQuery.noConflict()) around the following function

$(function() { jQuery.noConflict(); ... });

And be sure to add it to the <head> section of your page.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!