- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有几个地方包含内联 <script>
运行代码的 block ,由 $(document).ready()
包裹.其中几个 block 使用依赖于其他外部脚本的方法。在正常的页面执行过程中,这种技术工作正常,但是当用户在页面完全加载之前离开页面时,$(document).ready()
事件被触发,IE 抛出这些错误:
"Object does not support this property or method"
通过一些测试,我发现 window.beforeunload
事件在任何 ready
之前触发事件,所以我希望能够阻止 ready
事件在那时被触发。理想情况下,我希望有一个通用的解决方案,这样我就不必修改所有内联代码。
如果这不可能,您是否建议将所有内联代码包装在 try-catch
中? block ,在执行内联代码之前确定是否已加载所有外部脚本等?
最佳答案
如果插件是在事后加载的,那么内联脚本不应该像那样贯穿始终。
IMO,更好的方法是像这样设置您的页面:
<html>
<head>
<title></title>
</head>
<body>
<div id="content">
</div>
<script src="jquery.js"></script>
<script src="jquery.customerPlugins.js"></script>
<script>
$(function(){
// all inline would go here!
});
</script>
<script src="main.js"></script>
</body>
</html>
但如果这不是一个选项,您可以(但可能不应该)(下面未经测试的代码):
<html>
<head>
<title></title>
<script src="jquery.js"></script>
<script>
var readyQueue = [];
</script>
</head>
<body>
<script>
readyQueue.push(function(){
$("body").append("<div />").customPlugin();
});
</script>
<div id="content">
<script>
readyQueue.push(function(){
$("#content").customPlugin();
});
</script>
</div>
<script src="jquery.customPlugins.js"></script>
<script>
// do something like this...
$(function(){
var fn;
while(fn = readyQueue.shift()){
fn();
}
// if you want to continue to use readyQueue in code
// below/after this you could do something like the following:
readyQueue = {"push": function(fn){
fn();
})};
// this would execute immediately:
readyQueue.push(function(){
alert('Hello');
});
});
</script>
<script src="main.js"></script>
</body>
</html>
当你在做的时候,看看html5boilerplate.com .
关于javascript - .ready() 在 window.beforeunload 之后触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3915384/
我在 Chrome 和 Safari 上试过了,这两个浏览器都不行。当我刷新页面时,Chrome 中只有一个警告窗口如下所示。如果仅使用 evt.preventDefault(),则该函数不起作用。不
我的代码: //hold window open on form change window.onbeforeunload = function(e) { if(formChanges > 0
以下代码始终显示相同的消息:此页面要求您确认等。是否可以更改此文本以显示其他内容? $(window).bind('beforeunload', function(){ }); 是否可以查询用户是真的
我有一小段代码: $(window).bind('beforeunload', function() { $.ajax({ async: false, type: 'POST',
我想知道是否可以在 beforeunload 事件上向数据库发送查询。 $(window).on('beforeunload',function() { console.log('before
import React, { useState, useEffect } from "react"; import "./styles.css"; export default function A
我在移动浏览器上工作了大约 3 个月,现在我遇到了一个无聊的问题 beforeunload和 onload .在两种情况下,它们不能清楚地工作。当我关闭选项卡时,浏览器经常调用它们。但是,当用户通过一
第 1 页包含动态表单,第 2 页是对提交数据的审核。我有以下限制: 提交第 1 页时没有 beforeunload 警告; beforeunload 刷新页面 1 时发出警告; 从第 2 页发出 h
我已经使用 window.beforeunload 功能禁用了页面重定向/重新加载,但是一旦页面中发生表单提交,我想重定向到下一页,而不显示 window.beforeunload 警报。我怎样才能做
我有一个表单,当某些元素获得焦点时它会发生变化。 我希望在离开页面时收到警告。所以我保留了一个标志,告诉我这些元素何时发生变化。 问题在于,“此页面正在要求您确认是否要离开”警报始终提示; funct
我一直在思考应该使用哪一个:beforeunload或onbeforeunload它们似乎都在做非常相似的事情,但具有不同的浏览器兼容性。 一些背景: 我有一个表格。在页面加载时,我序列化表单并将其保
当用户单击按钮时,我使用 JqueryUI 自定义确认框。 这是脚本, function exit() { $("#dialog-confirm").dialog({
我正在尝试找出如何限制表单上的 beforeunload 命令。我已经能够弄清楚如何取消绑定(bind) beforeunload 命令,然后当我离开这个有效的表单时。但是,无论表单是否更改,每次都会
在我的应用程序中,我有一个评论框,我想在其中显示一条消息,表明它尚未保存。在函数 warnOpenEditor 中,我添加了 window.onbeforeunload 事件,但它不会触发。附上TS和
我有一个简单的函数,可以在发出新页面请求时显示旋转器叠加层。 $(window).on("beforeunload", function() { $('#nav-spinner').show();
我有一个主干应用程序,我想在导航到未保存的更改之前提醒用户。我有一个事件要做: $(window).bind('beforeunload', function() { var status =
我正在将 JSF 与omnifaces 和cdi 一起使用。在 xhtml 页面中,我使用了一些类似于(下面的 jsf 页面有更多详细信息) 的 js window.onbeforeunload =
我在我的页面上使用了 beforeunload 事件,这样当用户试图关闭我的网站/标签时,我会提示用户他们不应该离开网站,除非填写所有表格。但问题是我的页面有一个丰富的表单,在每个部分表单填写后重新加
我正在页面中检测表单更改,所以我已经处理了 $('#element').change(function(){ metaDataChange = true; }); 这样当用户离开页面并且在表单的输入中
我明白用自定义对话框替换 beforeunload 对话框是不可能的,如果我们需要为用户设置自定义消息,我们必须返回一个字符串我们的 beforeunload 处理程序: {Custom messag
我是一名优秀的程序员,十分优秀!