打开浏览器时加载 [英] Load when the browser is open

查看:95
本文介绍了打开浏览器时加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个特殊的按钮,通过使用此js使用api显示服务器状态,我的问题是,第一次打开浏览器时,我无法使此东西自动加载,因此必须实际单击该按钮否则它不会开始运行.

I made a special button that thru the use of this js shows the server status using a api, my problem is that i cant make this thing to automaticly load the first time you open the browser, you have to actually click the button otherwise it doesnt start runing.

function checkStatus(event)
{
    jQuery.getJSON("url",function(data){ 
        var button = document.getElementById("ServStat");
        if ( data.status == '1') { 
            jQuery('#ServStat').addClass('online').removeClass('offline');
            button.label = 'Online'; }
        else { 
            jQuery('#ServStat').addClass('offline').removeClass('online');
            button.label = 'Offline' 
        }
    });
}

任何想法都值得欢迎和感谢.

Any ideas are welcome and thanks in advance.

推荐答案

只需将代码添加到document.ready函数即可.文档完全加载后,将执行该命令.

Just add the code to document.ready function. It gets executed when the document has been load completely.

所以您的代码将变成类似这样的

So your code will become something like,

$(document).ready(function () {
    jQuery.getJSON("url", function (data) {
        var button = document.getElementById("ServStat");
        if (data.status == '1') {
            jQuery('#ServStat').addClass('online').removeClass('offline');
            button.label = 'Online';
        } else {
            jQuery('#ServStat').addClass('offline').removeClass('online');
            button.label = 'Offline'
        }
    });
});

您可以在此处获取更多详细信息: http://api.jquery.com/ready/

You can get more details here : http://api.jquery.com/ready/

这篇关于打开浏览器时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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