【问题标题】:Cookie is not set in Internet Explorer, gives an errorInternet Explorer 中未设置 Cookie,出现错误
【发布时间】:2015-03-19 17:05:13
【问题描述】:

我一直在使用下面的代码来创建一个 cookie,除了 INTERNET EXPLORER 之外的所有浏览器都可以正常工作。当我点击创建 cookie 按钮时,它会出现以下错误。

错误

Message: '$' is undefined
Line: 4
Char: 2
Code: 0
URI: http://localhost/js/scripts.js

代码

<p>Click on the <strong>Set Cookie</strong> button below to create the testing cookie.</p><br>
            <button style="color: black" input type="button" onclick="set_cookie(<?php echo "'".$set_cookie_url."'" ?> );" <?php if(isset($_COOKIE["cookieid"])) { ?> Disabled <?php } ?>value='Set Cookie'><b>Set cookie</b></button>

JS 代码

function set_cookie(url){
    $.get( url, { cookieid: "628929", projectid: "1" }, set_cookie_complete);
    $('body').modal('show') ;
}
function set_cookie_complete(){
    location.reload();  
}

function unset_cookie(url){
    $.get( url, { cookieid: "628929", projectid: "1" }, unset_cookie_complete);
    $('body').modal('show') ;
}
function unset_cookie_complete(){
    location.reload();  
}

谁能帮我摆脱这个错误并设置 cookie

【问题讨论】:

  • 请注意:在 echo 命令中使用单引号 ( '\'' ) 表示字符串和逗号 ( , ),这将使您的 PHP 代码运行得更快 ;)
  • 好的,但是你说的和上面的问题没有关系吧?
  • 是的,很可能。您的问题出在 JS 中,而不是 PHP 中。 jQuery 库是否正确加载?
  • 我对 jquery 很陌生,我只使用这个链接来加载库
  • 你用什么IE? jQuery 2.x 不支持旧版本!尝试 IE10 或 IE11(如果可能)。如果没有,你必须使用jquery 1.11.x

标签: javascript php jquery internet-explorer cookies


【解决方案1】:

对于 IE8 和更早版本,您不能使用 jQuery 2.x

将您的链接更改为 1.11.x 版本。对于你使用的这么简单的代码,应该没有区别。

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

注意:在 URL 的开头使用“//”有助于浏览器决定如何处理它并允许同时使用 HTTP 或 HTTPS。不使用它可能会导致 url "http://your.server.com/ajax.googleapis.com/ajax/..."

【讨论】:

  • 一件小事.. 还有另一个错误说“对象不支持此属性或方法”.. 你知道这是怎么回事吗
  • 当您打开 IE 调试器(按 F12)时,它应该停止出现错误,这将告诉您问题的确切方法。 (如果您使用任何其他 jQuery 插件,您可能需要获取与 1.x 兼容的旧版本。)
猜你喜欢
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 1970-01-01
  • 2013-03-23
  • 2014-01-30
  • 2012-01-02
相关资源
最近更新 更多