1

I am trying to call a JavaScript function after load...like totally after the page loads. 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. The page then prints some javascript, but the page has already loaded. 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. I have also tried heredoc, and it didn't work either.

2

3 Answers 3

1

You want the window.load() event. Here's a jQuery example;

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

You could also call this on a specific tag (<body>, for example) as onload="".

<body onload="...">
Sign up to request clarification or add additional context in comments.

3 Comments

I have researched stuff and seen that. That won't work, but thanks for the suggestion.
Ooohh, now that you added your code it's clearer; asynchronous queries don't wait, they are designed that way. However, see... stackoverflow.com/questions/3709597/…
Yes, I know they are designed that way, but this is added to page after load, and it never executes. I need a way to fix that. Thanks again, but not quite what I need.
1

jQuery

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

JavaScript

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

1 Comment

I know about both, and neither will do. It's for a chat, so this is the way I am retrieving new chat messges.
1

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. I need to move this over to the client and to JavaScript. Thanks though, and sorry for posting an impossible question!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.