jQuery $.cookie 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18024539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery $.cookie is not a function
提问by ElliotSchmelliot
I am trying to set a cookie using jQuery:
我正在尝试使用 jQuery 设置 cookie:
$.cookie("testCookie", "hello");
alert($.cookie("testCookie"));
But when I load my page, I receive the error "$.cookie is not a function". Here is what I know:
但是当我加载我的页面时,我收到错误“$.cookie is not a function”。这是我所知道的:
- I have downloaded the jQuery cookie plugin here.
- I am linking to jQuery and THEN the cookie plugin.
- Both jQuery and jQuery.cookie are loading correctly with 200 OKs.
- 我已经在这里下载了 jQuery cookie 插件。
- 我正在链接到 jQuery,然后是 cookie 插件。
- jQuery 和 jQuery.cookie 都以 200 OK 正确加载。
I have looked at several other answers (hereand hereamong others), to which most people suggested renaming the cookie.js file. I have renamed my cookie file "jquery.cookeee.js" but the results are the same.
我查看了其他几个答案(这里和这里等等),大多数人建议重命名 cookie.js 文件。我已将我的 cookie 文件重命名为“jquery.cookeee.js”,但结果是一样的。
Any ideas on what is going on here?
关于这里发生了什么的任何想法?
If it helps, I am creating a web application in MVC 4.
如果有帮助,我将在 MVC 4 中创建一个 Web 应用程序。
回答by ElliotSchmelliot
Here are all the possible problems/solutions I have come across:
以下是我遇到的所有可能的问题/解决方案:
1. Download the cookie plugin
1.下载cookie插件
$.cookie
is not a standard jQuery function and the plugin needs to be downloaded here. Make sure to include the appropriate <script>
tag where necessary (see next).
$.cookie
不是标准的 jQuery 函数,插件需要在这里下载。确保<script>
在必要时包含适当的标签(见下文)。
2. Include jQuery before the cookie plugin
2. 在 cookie 插件之前包含 jQuery
When including the cookie script, make sure to include jQuery FIRST, then the cookie plugin.
当包含 cookie 脚本时,确保首先包含 jQuery,然后是 cookie 插件。
<script src="~/Scripts/jquery-2.0.3.js" type="text/javascript"></script>
<script src="~/Scripts/jquery_cookie.js" type="text/javascript"></script>
3. Don't include jQuery more than once
3. 不要多次包含 jQuery
This was my problem. Make sure you aren't including jQuery more than once. If you are, it is possible that:
这是我的问题。确保您不会多次包含 jQuery。如果您是,则可能:
- jQuery loads correctly.
- The cookie plugin loads correctly.
- Your second inclusion of jQuery overwrites the first and destroys the cookie plugin.
- jQuery 正确加载。
- cookie 插件正确加载。
- 您第二次加入 jQuery 会覆盖第一个并破坏 cookie 插件。
For anyone using ASP.Net MVC projects, be careful with the default javascript bundle inclusions. My second inclusion of jQuery was within one of my global layout pages under the line @Scripts.Render("~/bundles/jquery")
.
对于使用 ASP.Net MVC 项目的任何人,请注意默认的 javascript 包包含。我第二次包含 jQuery 是在我的全局布局页面之一中@Scripts.Render("~/bundles/jquery")
。
4. Rename the plugin file to not include ".cookie"
4. 将插件文件重命名为不包含“.cookie”
In some rare cases, renaming the file to something that does NOT include ".cookie" has fixed this error, apparently due to web server issues. By default, the downloaded script is titled "jquery.cookie.js" but try renaming it to something like "jquery_cookie.js" as shown above. More details on this problem are here.
在极少数情况下,将文件重命名为不包含“.cookie”的内容已修复此错误,这显然是由于 Web 服务器问题。默认情况下,下载的脚本标题为“jquery.cookie.js”,但尝试将其重命名为“jquery_cookie.js”,如上所示。关于这个问题的更多细节在这里。
回答by YPCrumble
The old versionof jQuery Cookie has been deprecated, so if you're getting the error:
在老版本的jQuery的Cookie已被弃用,因此,如果你得到的错误:
$.cookie is not a function
you should upgrade to the new version.
你应该升级到新版本。
The API for the new version is also different - rather than using
新版本的 API 也不同——而不是使用
$.cookie("yourCookieName");
you should use
你应该使用
Cookies.get("yourCookieName");
回答by cabs
add this cookie plugin for jquery.
为 jquery 添加这个 cookie 插件。
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
回答by Tatyana
Check that you included the script in header and not in footer of the page. Particularly in WordPress this one didn't work:
检查您是否将脚本包含在页眉中而不是页脚中。特别是在 WordPress 中,这个不起作用:
wp_register_script('cookie', get_template_directory_uri() . '/js/jquery.cookie.js', array(), false, true);
The last parameter indicates including the script in footer and needed to be changed to false (default). The way it worked:
最后一个参数表示将脚本包含在页脚中,需要更改为 false(默认)。它的工作方式:
wp_register_script('cookie', get_template_directory_uri() . '/js/jquery.cookie.js');
回答by Amit Mishra
You should add first jquery.cookie.js
then add your js or jQuery where you are using that function.
您应该先添加,jquery.cookie.js
然后在使用该函数的位置添加 js 或 jQuery。
When browser loads the webpage first it loads this jquery.cookie.js
and after then you js or jQuery and now that function is available for use
当浏览器首先加载网页时,它会加载这个jquery.cookie.js
,然后加载js 或 jQuery,现在该功能可供使用
回答by Kostas Tsakalidis
I had this problem as well. I found out that having a $(document).ready function that included a $.cookie in a script tag inside body while having cookie js load in the head BELOW jquery as intended resulted in $(document).ready beeing processed before the cookie plugin could finish loading.
我也有这个问题。我发现有一个 $(document).ready 函数,它在 body 内的脚本标签中包含一个 $.cookie,同时在头部 BELOW jquery 中加载 cookie js 导致 $(document).ready 在 cookie 之前被处理插件可以完成加载。
I moved the cookie plugin load script in the body before the $(document).ready script and the error disappeared :D
我在 $(document).ready 脚本之前移动了正文中的 cookie 插件加载脚本,错误消失了:D
回答by Shivendra Kumar Singh
Solve jQuery $.cookie is not a function this Problem jquery cdn update in solve this problem
解决jQuery $.cookie is not a function 这个问题jquery cdn update in解决这个问题
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=" crossorigin="anonymous"></script>