附加的javascript在Safari浏览器中不起作用。safari javascript问题
此脚本在导航离开当前页面之前显示一条确认消息。
令人惊讶的是,这是第一次在safari中工作(但不是在随后的提交过程中)。
场景:
- 用户进行一些更改,然后按提交按钮
- 确认消息显示给用户
- 用户决定按下取消按钮退后同一页面
- 但后记用户上无法再次调用提交。
- 提交按钮不起作用。
P.S:此代码完美适用于其他浏览器,即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>
在这里回答? http://stackoverflow.com/questions/803887/can-i-pop-up-a-confirmation-dialog-when-the-user-is-closing-the-window-in-safari – Mantar 2010-10-25 12:08:04