Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery unload():更新Cookie后出现语法错误-如何查找原因?_Javascript_Jquery_Cookies - Fatal编程技术网

Javascript jQuery unload():更新Cookie后出现语法错误-如何查找原因?

Javascript jQuery unload():更新Cookie后出现语法错误-如何查找原因?,javascript,jquery,cookies,Javascript,Jquery,Cookies,下面的解决方案:编辑#2 我有一个用户可以排序的HTML列表。我不想在每次拖放操作后保存数据,所以我在卸载时保存数据:在cookie和数据库中。这是可行的,但是: 保存数据后,列表被隐藏,我在这一行得到一个“语法错误”: <!DOCTYPE html> 你知道如何找到这个错误吗?有什么工具或策略吗?或者与卸载功能有相同的体验 提前谢谢 编辑: 一些代码: var list_is_dirty = false; // document ready? $(function() {

下面的解决方案:编辑#2

我有一个用户可以排序的HTML列表。我不想在每次拖放操作后保存数据,所以我在卸载时保存数据:在cookie和数据库中。这是可行的,但是:

保存数据后,列表被隐藏,我在这一行得到一个“语法错误”:

<!DOCTYPE html>
你知道如何找到这个错误吗?有什么工具或策略吗?或者与卸载功能有相同的体验

提前谢谢

编辑:

一些代码:

var list_is_dirty = false;

// document ready?
$(function() {

    function sort_list() {
        // some code, not important
    }

    sort_list();

    $(window).unload(function() {
        if (list_is_dirty == true) {

            /* ---------- HERE's the error! ---------- */
            /* The error occures when I try to call the script.php 
               I tried load(), $.post(), $.get() but nothing works.
               The string is correct. I'm not even able to call any of 
               these functions without params. 
            */

            // send data to script.php to save data 
            $("#div").load("script.php?str="+list_data_str); 

            $.cookie("list_data", list_data_str);
        }
    });
}
编辑#2/解决方案: 我不知道为什么,但一切都是使用window.onbeforeunload而不是jQuery.unload()进行的。解释一下就好了!我很抱歉这条令人困惑的线索

window.onbeforeunload = function(e) {
    $("#div").load("script.php");
}

我认为你的问题在于:列出数据,因为它在任何地方都没有定义。 例如,如果您想说您想做AJAX post,那么显然您需要寻找成功事件 否则,您的演示代码似乎遗漏了一些东西,因为如果在接收脚本中使用$\u忽略URL,而不注意任何参数,您可以按照尝试的方式来执行。。换句话说,您缺少该对象,当您刷新页面时,它将加载到DOM中。显然,这可能是您正在描述的问题,我建议您发布更多与您的问题代码相关的内容。。类似于接收脚本或来自Firebug等调试器的任何错误

关于如何测试它,您可能希望在受支持的浏览器中使用console.log,或者在设置cookie时发出警报

         var global list_is_dirty = false; 

              function sort_list(list, list_is_dirty) {
                // some code, not important
            //check the list and the flag 
            //you should return a value, else it does not make sense to use a function here.. note the var defined as global
            return list;  //?? not sure what to return as don't know what this code does from the posting
            }



              jQuery(function($)
              {

                $(window).load(function(e){

                                var list_data_str= sort_list( );


                        // send data to script.php to save data 
                        e("#div").load('script.php?str='+list_data_str); 

                                    //on unload destroy the cookie perhaps?? or if it's not set a session variable
                        e.cookie("list_data", list_data_str);

                ...


                            The unload event

                                $(window).unload(function(e) {


                              $("#div").load("script.php?str="+list_data_str); 

                                 $.cookie("list_data", list_data_str);
                                   }

                                });
                            }
                            ....
//关于编辑:您是否在此处向脚本传递任何参数?因为我认为问题就在于这个逻辑。 window.onbeforeunload=函数(e){
$(“#div”).load(“script.php”)

需要更多的实际代码/错误,更少的解释。@melpomene谢谢你的回答。我试图找出重要的几行。@aug我认为你走对了。我想是这样的。我会在几分钟后发布一些代码。谢谢!发布所有代码,而不是一些代码如何?错误也会很好。@melpomene我不得不这样做发布过多代码(公司实习生库)。但我确信错误与卸载函数有关。cookie更改时没有问题。我只是无法在卸载函数中调用另一个脚本。不幸的是,这是主要问题。我发现了错误,但没有找到解决方案。我希望您没事。感谢您的详细回答!代码不是独立运行的,我是jUT发布它给你一个相当的概览。我从这个页面得到了使用WONDOW.ONBEFELEOLAD的想法:有趣……我很想看到一个演示,如果你可以把它放在JSFIDLE中…我相信如果你想打开一个模态对话框窗口,还有一些其他的小细节你应该考虑。ID以避免隐藏多个实例等。当时,如果您正在寻找的是检查对话框模式是否已卸载,这将更有意义。如果您正在处理某些处理,例如从对话框窗口提交表单等。无论如何,这是一个有趣的主题。
         var global list_is_dirty = false; 

              function sort_list(list, list_is_dirty) {
                // some code, not important
            //check the list and the flag 
            //you should return a value, else it does not make sense to use a function here.. note the var defined as global
            return list;  //?? not sure what to return as don't know what this code does from the posting
            }



              jQuery(function($)
              {

                $(window).load(function(e){

                                var list_data_str= sort_list( );


                        // send data to script.php to save data 
                        e("#div").load('script.php?str='+list_data_str); 

                                    //on unload destroy the cookie perhaps?? or if it's not set a session variable
                        e.cookie("list_data", list_data_str);

                ...


                            The unload event

                                $(window).unload(function(e) {


                              $("#div").load("script.php?str="+list_data_str); 

                                 $.cookie("list_data", list_data_str);
                                   }

                                });
                            }
                            ....