Safari JavaScript 问题

发布于 2024-09-29 02:53:16 字数 1643 浏览 3 评论 0原文

附加的 javascript 在 safari 中不起作用。

该脚本在离开当前页面之前显示一条确认消息。

令人惊讶的是,这在 Safari 中第一次起作用(但在随后的提交过程中不起作用)。

场景:

  • 用户进行一些更改并按提交按钮
  • 向用户显示确认消息
  • 用户决定按取消按钮返回同一页面,
  • 但事后用户无法再次调用提交。
  • 提交按钮不起作用。

PS:此代码与其他浏览器(IE7/8、FireFox)完美配合。

 function checksave() {
  if (formIsDirty(myForm)) {
   return "The form has been changed...";
  }
 }

 function formIsDirty(form) {
  for (var i = 0; i < form.elements.length; i++) {
   var element = form.elements[i];
   var type = element.type;
   if (type == "checkbox" || type == "radio") {
    if (element.checked != element.defaultChecked) {
     return true;
    }
   }
   else if (type == "hidden" || type == "password" || type == "text" ||
     type == "textarea") {
    if (element.value != element.defaultValue) {
     return true;
    }
   }
   else if (type == "select-one" || type == "select-multiple") {
    for (var j = 0; j < element.options.length; j++) {
     if (element.options[j].selected !=
     element.options[j].defaultSelected) {
      return true;
     }
    }
   }
  }
  return false;
 }

<body OnBeforeUnload ="return checksave();">
 <form id="myForm" >
  <table border="1">
   <tr> <td>Enter Text:</td><td><input id="text1" type="text" value=""/> <br/> </td></tr>
   <tr> <td><input id="button1" type="submit" value="Submit1"/> <br/> </td> <td><input id="button2" type="submit" value="Submit2"/> <br/> </td></tr>
  </table>
 </form>
</body>

attached javascript does not work in safari.

this script displays a confirmation message before navigating away from the current page.

surprisingly this works for the first time in safari (but not during subsequent submit).

Scenario:

  • user makes some changes and press submit button
  • confirmation message displayed to user
  • user decided to press cancel button to stay back on the same page
  • but afterword user unable to invoke submit again.
  • submit button does not work.

P.S: This code works perfectly with othere browser i.e. IE7/8, FireFox.

 function checksave() {
  if (formIsDirty(myForm)) {
   return "The form has been changed...";
  }
 }

 function formIsDirty(form) {
  for (var i = 0; i < form.elements.length; i++) {
   var element = form.elements[i];
   var type = element.type;
   if (type == "checkbox" || type == "radio") {
    if (element.checked != element.defaultChecked) {
     return true;
    }
   }
   else if (type == "hidden" || type == "password" || type == "text" ||
     type == "textarea") {
    if (element.value != element.defaultValue) {
     return true;
    }
   }
   else if (type == "select-one" || type == "select-multiple") {
    for (var j = 0; j < element.options.length; j++) {
     if (element.options[j].selected !=
     element.options[j].defaultSelected) {
      return true;
     }
    }
   }
  }
  return false;
 }

<body OnBeforeUnload ="return checksave();">
 <form id="myForm" >
  <table border="1">
   <tr> <td>Enter Text:</td><td><input id="text1" type="text" value=""/> <br/> </td></tr>
   <tr> <td><input id="button1" type="submit" value="Submit1"/> <br/> </td> <td><input id="button2" type="submit" value="Submit2"/> <br/> </td></tr>
  </table>
 </form>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2024-10-06 02:53:16

是的,这是 WebKit 中的一个 bug,尽管该错误似乎已在当前版本中修复适合我的 Chrome 版本。

似乎当从 beforeunload 提示中取消表单提交时,页面上的所有表单都将变得不可提交(submit 事件仍会触发,但提交不会导航),直到页面上的任何表单字段发生更改。 (这与您自己的表单更改检查无关。浏览器本身会阻止导航,直到注意到更改为止。也许某种表单多次提交预防技巧出了问题,或者是什么?)

通常您不会请注意,因为通常您会在 form.onsubmit 上放置一个不同的处理程序,这会使 onbeforeunload 短路(因为通常如果用户单击提交表单,他们< em>知道他们已经更改了一些内容,并且想要保存更改)。

(顺便说一句,避免使用 myForm 作为全局变量来引用带有 id="myForm 的元素。这是一些其他浏览器现在已经实现的 IE hack,但仅在Quirks 模式,并且您想要处于 Quirks 模式。添加 声明并使用 document.getElementById('myForm')。。)

Yeah, it's a bug in WebKit, though one that seems to be fixed in current Chrome versions for me.

Seems when a form submission is cancelled from a beforeunload prompt, all forms on the page become unsubmittable (the submit event still fires but the submission does not navigate) until any form field on the page is changed. (This is nothing to do with your own form-changedness checking. The browser itself blocks the navigation until it notices a change. Maybe some kind of form-multiple-submission-prevention trick gone wrong, or something?)

Normally you wouldn't notice since typically you'd put a different handler on form.onsubmit which would short-circuit the onbeforeunload (since generally if the user is clicking to submit the form, they know they've changed something on it, and want to save the change).

(Incidentally, avoid use of myForm as a global variable to reference the element with id="myForm. This is an IE hack that some other browsers have now implemented but only in Quirks Mode, and you don't want to be in Quirks Mode. Add a <!DOCTYPE> declaration and use document.getElementById('myForm').)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文