如何使用javascript验证网页是否已完全加载 [英] How to verify if a webpage is completely loaded using javascript

查看:82
本文介绍了如何使用javascript验证网页是否已完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在< ahref> 中禁用我正在使用的图片,直到页面完全加载为止。

I need to disable an image which I am using in <a "href"> until the page completely loads.

我不能使用 document.ready()因为我需要在文档准备好之前禁用它。

I cannot use document.ready() because I need to disable the the BEFORE the document is ready.

有人可以帮我吗?

问候,
Gnanesh

Regards, Gnanesh

推荐答案

在HTML中将其定义为已禁用:

Define it in your HTML as disabled:

<button disabled="disabled">My Button</button>

然后在页面加载时重新启用它。

And then on page load re-enable it.

这对于没有Javascript的用户而言具有破坏功能的缺点。另一种方法是在按钮后直接添加一小段代码:

This has the downside of breaking functionality for users without Javascript. The other way to do it is to add a small line of code directly after the button:

<button id="myButton">My Button</button>

<script type="text/javascript">
    document.getElementById('myButton').disabled = true;
</script>

...然后在document.load()上重新启用

...and then re-enable on document.load()

使用新信息进行修改

是否为输入,类型为image?如果是这样,以上仍然有效。如果没有,并且它是一个带有图像的< a> 标签,我建议不要接受已接受的答案,抱歉。在最后突然出现图像可能会非常令人沮丧或分心,考虑到页面需要很长时间才能加载,您需要禁用链接。我建议改为:

Edit with new info:
Is it an input with type "image"? If so, the above will still work. If not, and it's an <a> tag with an image inside it, I wouldn't recommend doing what the accepted answer suggests, sorry. Having an image suddenly appear at the end could get quite frustrating or distracting, considering that the page takes so long to load that you need to disable the link. What I'd suggest instead is this:

<a href="whatever" onclick="return myLinkHandler();"><img src="..." /></a>
<script type="text/javascript">
    var myLinkHandler = function() {
        alert('Page not loaded.');  // or something nicer
        return false;
    };
</script>

然后在你的document.ready函数中,更改函数的定义:

and then in your document.ready function, change the definition of the function:

myLinkHandler = function() {
    alert("Yay, page loaded.");
    return true;
};

或者,您可以在函数内部检查以查看页面是否已加载。

Alternatively, you could put a check inside the function to see if the page has loaded or not.

var documentReady = false;
function myLinkHandler() {
    alert (documentReady ? "Ready!" : "Not ready!");
    return documentReady;
}

document.onload = function () { // or use jQuery or whatever
    documentReady = true;
};

这篇关于如何使用javascript验证网页是否已完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆