Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.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 当textarea不为空时,如何在关闭页面前警告用户?_Javascript_Html - Fatal编程技术网

Javascript 当textarea不为空时,如何在关闭页面前警告用户?

Javascript 当textarea不为空时,如何在关闭页面前警告用户?,javascript,html,Javascript,Html,我试图复制与stackoverflow文本区域相同的内容 编辑:在阅读了这里的建议后,我选择了这样的方式: window.onbeforeunload = function() { var myTextArea = document.getElementById('post_content'); if (myTextArea.value.length > 0) { return "You haven\'t submitted your post;

我试图复制与stackoverflow文本区域相同的内容

编辑:在阅读了这里的建议后,我选择了这样的方式:

window.onbeforeunload = function()
{

    var myTextArea = document.getElementById('post_content');

    if (myTextArea.value.length > 0)
    {
        return "You haven\'t submitted your post; are you sure you want to discard it?"; 
    }

}
这似乎适用于Firefox和其他浏览器,而不会引起双重确认。

使用类似于:

window.onunload = function(){
   var myTextArea = [a ref to your textarea];
   if (myTextArea.value.length > 0) {
   //=>textarea contains text, ask the user:
      return confirm('You didn\'t submit your text yet! Are you sure'+
                     ' you want to navigate away from this page?');
   }
   //=>continue unloading
   return true;
}