简体   繁体   English

页面完全加载后调用JavaScript函数

[英]Call JavaScript function after page completely loads

I am trying to call a JavaScript function after load...like totally after the page loads. 我试图在加载后调用JavaScript函数,就像完全在页面加载后一样。 It's part of a chatroom I'm designing, in which a cron repeatedly checks for new data, and if there's new data, it tells the chatroom index.php that there's new data. 它是我正在设计的聊天室的一部分,其中cron反复检查新数据,如果有新数据,它会告诉聊天室index.php有新数据。 The page then prints some javascript, but the page has already loaded. 该页面然后打印一些javascript,但是该页面已经加载。 How do I call the functions and lines of execution after the page has loaded? 页面加载后如何调用函数和执行行? Thanks so much in advance! 非常感谢!

EDIT: Here's the code: 编辑:这是代码:

if($connection) {
    $sql = "SELECT * FROM `refresh` WHERE `refresh`=1;";
    $query = mysqli_query($connection, $sql);
    $result = mysqli_fetch_fields($query);
    if($result !== null) {
        ?>
        <script type="text/javascript">
            refreshChat();
            $.ajax({url: "chat.log", type: "POST"})
            .done(function (data) {
                $("#chattxt").html(data);
            });
        </script>           
<?php
                }
            }
//More code irrelevant to this problem.

The JavaScript function refreshChat() is simply the same code I put after the call to it. JavaScript函数refreshChat()与我在调用它之后放置的代码完全相同。 I have also tried heredoc, and it didn't work either. 我也尝试过heredoc,但也没有用。

You want the window.load() event. 您需要window.load()事件。 Here's a jQuery example; 这是一个jQuery示例;

jQuery(window).load(function () {
    // page loaded, do stuff
});

You could also call this on a specific tag ( <body> , for example) as onload="" . 您还可以在特定标签(例如<body> )上将其称为onload=""

<body onload="...">

jQuery jQuery的

$(document).ready(function() {
  // DOM Loaded
});

JavaScript 的JavaScript

document.onload = function() {
  // DOM loaded
};

After sitting on the problem and thinking about it, I realized that what I was trying to do won't work. 在解决问题并进行思考之后,我意识到我试图做的事情行不通。 The PHP is translated before the page is loaded on the screen at the server, and then after it loads, nothing else can happen. 在将页面加载到服务器的屏幕上之前,将翻译PHP,然后在加载页面之后,再也不会发生其他事情。 I need to move this over to the client and to JavaScript. 我需要将其移至客户端和JavaScript。 Thanks though, and sorry for posting an impossible question! 不过,谢谢您,并为发布一个不可能的问题感到抱歉!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM