Reputation: 21
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.
Upvotes: 1
Views: 628
Reputation: 21
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!
Upvotes: 1
Reputation: 223
jQuery
$(document).ready(function() {
// DOM Loaded
});
JavaScript
document.onload = function() {
// DOM loaded
};
Upvotes: 1
Reputation: 2355
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="...">
Upvotes: 1