...

View Full Version : Body onload



Dan13071992
12-16-2011, 08:39 PM
hi guys, how can i get this code to load on body onload? without fading in and out?



<html>
<head>
<!-- For ease i'm just using a JQuery version hosted by JQuery- you can download any version and link to it locally -->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$("#responsecontainer").load("response.php");
var refreshId = setInterval(function() {
$("#responsecontainer").load('response.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
});
</script>
</head>
<body>

<div id="responsecontainer">
</div>
</body>



Thanks :)

devnull69
12-16-2011, 09:07 PM
There is no "fading in or out" in this code ...

Dan13071992
12-16-2011, 09:08 PM
sorry i realised that after i posted, i just need to know how to get it to load the page on page load, as its not loading hte page at all :S

cuzMazn
12-16-2011, 09:17 PM
sorry i realised that after i posted, i just need to know how to get it to load the page on page load, as its not loading hte page at all :S

Hi there,

[Please tell me if I misunderstand your question]

I am not really sure what are your problems, the page should be rendered completely before the code can be executed using the .ready() syntax:



$(document).ready(function() {
//You code here WILL be executed when the page is completely rendered
});


However, if you want the page to load completely, for all elements not only source code (such as images, flash...), use $(window) instead of $(document):



$(window).ready(function() {
//You code here WILL be executed when the page is completely loaded, including images, flash...
});


Tim_

Dan13071992
12-16-2011, 09:21 PM
i know have this



<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(window).ready(function() {
$("#responsecontainer").load("inc-mess-jail.php");
var refreshId = setInterval(function() {
$("#responsecontainer").load('inc-mess-jail.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
});
</script>

<div id="responsecontainer">
</div>


thats not the full html as the div is inside html ect but alot of it.

and in the inc-mess-jail.php file i have this:



<?php
include('../includes/db_connect.php');
include('../includes/inc-logincheck.php');
include('../includes/inc-functions.php');
include('../includes/inc-userfunctions.php');

$intUserID=$_SESSION['uID'];
$fetch=$user;


$newMail = mysql_query("SELECT * FROM `inbox` WHERE `to`='$fetch->username' AND `read`='0'");
$newMail_rows = mysql_num_rows($newMail);
$inJail = mysql_query("SELECT * FROM `jail` WHERE `location`='$fetchlocation->country'");
$inJail_rows = mysql_num_rows($inJail);

$mailMessage = '<a href="../inbox.php"><img align="left" src="../images/icons/'.($newMail_rows==0 ? 'no' : 'new').'Mail.gif" border="0" width="45" height="45"></a>';

$jailIcon = '<a href="../jail.php"><img align="left" src="../images/icons/'.($inJail_rows>=1 ? 'in' : 'empty').'Jail.gif" border="0" width="45" height="45"></a>';

?>

<tr>
<td colspan="2" align="center">

<table border="0" cellspacing="0" cellpadding="0" width="75%">
<tr>
<td width="50%" align="center" valign="center">
<div id=messages><?php echo $mailMessage; ?></div>
</td>

<td width="50%" align="center" valign="center">
<div id=jail><?php echo $jailIcon; ?></div>
</td>
</tr>
</table>

<span id="errorMsg"></span>

</td>
</tr>


however its not loading the div at all, and not even sjhowing an error message :s

cuzMazn
12-16-2011, 09:28 PM
however its not loading the div at all, and not even sjhowing an error message :s

Hi Dan,

There could be various ways the errors take place, I suggest first of all, test the page inc-mess-jail.php alone first. Make sure that it will display what you expected, because whatever you saw there, will be exactly what you get using $.load(). If the problem is not from the PHP file, then we will have to mess with the JS file.

Tim_

Dan13071992
12-16-2011, 09:29 PM
hi tim, i have tested the inc-mess-jail.php file and it all works fine :) just the javascript/jquery thats not loading for some strange reason, im no good at javascript, or jquery and this is my first real attempt at it

cuzMazn
12-16-2011, 09:33 PM
hi tim, i have tested the inc-mess-jail.php file and it all works fine :) just the javascript/jquery thats not loading for some strange reason, im no good at javascript, or jquery and this is my first real attempt at it

Cool, so now we will have to work on the JS file.

Replace your:



$("#responsecontainer").load("inc-mess-jail.php");


By:



$("#responsecontainer").load("inc-mess-jail.php", function(response, status, xhr) {
if (status == "error") {
alert("Error: " + xhr.status + " " + xhr.statusText);
}
});


And tell me what you got.

Tim_

Dan13071992
12-16-2011, 09:36 PM
im getting error 404 not found, my inc-footer.php page, which this javascript is being used in is in the includes folder, along ith inc-mess-jail.php could it be that i need to write includes/inc-mess-jail.php instead of inc-mess-jail.php ??

cuzMazn
12-16-2011, 09:40 PM
im getting error 404 not found, my inc-footer.php page, which this javascript is being used in is in the includes folder, along ith inc-mess-jail.php could it be that i need to write includes/inc-mess-jail.php instead of inc-mess-jail.php ??

The 404 Error Not found indicates your "inc-mess-jail.php" could not be found. Make sure that you have the correct path, or at least try the absolute path. You can also post here your folder structure, maybe I can help.

The 404 Error alerted is not for the include() file in PHP. Because first, you run the inc-mess-jail.php alone and it works. If something is wrong, inc-mess-jail.php will display a message say that it "could not be found something blah blah blah", and you will receive exactly ("could not be found something blah blah blah") the same message when you use Ajax. Remember, whatever you see in the PHP file will be exactly what the browser receives after ajaxing.

Tim_

Dan13071992
12-16-2011, 10:01 PM
ok, i got it working, however now its showing above my javascript clock :s its ment to be displayed under it :s

http://crimewave.360-tactics.co.uk/clock.gif

the div is positioned below the clock in the html :s so i dont know why its coming up above it :S

cuzMazn
12-16-2011, 10:07 PM
ok, i got it working, however now its showing above my javascript clock :s its ment to be displayed under it :s

http://crimewave.360-tactics.co.uk/clock.gif

the div is positioned below the clock in the html :s so i dont know why its coming up above it :S

Do you have a live demo or can you upload your code? It's hard to tell about CSS just by gathering information. Just in case you don't have your code available, try to use Firebug or Google developer Tools to inspect your elements, your clock... and make sure the HTML code is as expected.

Tim_

Dan13071992
12-16-2011, 10:13 PM
that little block in the picture is diplayed like so:



<table border="0" width="120" cellspacing="0" cellpadding="0" class="cat">
<tr>
<td class="menu_header" width="120" height="15"><span class="menu_text">&nbsp;<img src="/game/site/site/bullet.gif"> <b>INFORMATION</b></span></td>
</tr>
<td>
<img src="/game/site/site/table_top.gif"><br>

<table border="0" cellspacing="0" cellpadding="0" width="100%" class="cat">
<tr>

<td colspan="2" align="center"><?php echo date('l'); ?><br /><span id="clock">&nbsp;</span></td>
</tr>
<tr>
<td colspan="2" align="center"><div id="1"><?php echo "<a href='trainstation.php'>$fetchlocatfooterion->country</a>"; ?></div></td>
</tr>





<center>

<div id="responsecontainer">
</div>


</center>






</table>


</td>
</tr>



</table>



the javascript you already know to that part.

below is the inc-mess-jail.php file:




<?php
include('../includes/db_connect.php');
include('../includes/inc-logincheck.php');
include('../includes/inc-functions.php');
include('../includes/inc-userfunctions.php');

$intUserID=$_SESSION['uID'];
$fetch=$user;


$newMail = mysql_query("SELECT * FROM `inbox` WHERE `to`='$fetch->username' AND `read`='0'");
$newMail_rows = mysql_num_rows($newMail);
$inJail = mysql_query("SELECT * FROM `jail` WHERE `location`='$fetchlocation->country'");
$inJail_rows = mysql_num_rows($inJail);

$mailMessage = '<a href="../inbox.php"><img align="left" src="../images/icons/'.($newMail_rows==0 ? 'no' : 'new').'Mail.gif" border="0" width="45" height="45"></a>';

$jailIcon = '<a href="../jail.php"><img align="left" src="../images/icons/'.($inJail_rows>=1 ? 'in' : 'empty').'Jail.gif" border="0" width="45" height="45"></a>';

?>
<link rel=stylesheet href=includes/screen.css type=text/css>
<tr>
<td colspan="2" align="center">

<table border="0" cellspacing="0" cellpadding="0" width="75%">
<tr>

<td width="50%" valign="center">
<?php echo $mailMessage; ?>
</td>

<td width="50%" valign="center">
<?php echo $jailIcon; ?>
</td>

</tr>
</table>

</td>
</tr>

cuzMazn
12-16-2011, 10:24 PM
Do you have any CSS for #responseconainer?

Try this: replace:



<center>

<div id="responsecontainer">
</div>


</center>


By:



<tr>
<td colspan="2">
<div id="responsecontainer" align="center"></div>
</td>
</tr>


The above code simply add 1 more rows to the existed table, so it can make sure that the <div> always stay under all other above table rows.

Tim_

Dan13071992
12-16-2011, 10:30 PM
that worked a treat :) thanks :) it had been bugging me lol. thanks for all of your help, i have other javascript projects to do, such as integrating php into javascript to creat a forum using iframes that load the pages, from the left iframe into the right hand iframe, i have all the javascript codes and php codes, and as you can tell im no good at javascript, so i needed some help intergrating them :s.

thanks for all your help on this topic. it was a big help, however is there a way to create a random javascipt pop up randomly after the user has gone to a different page say for example, the pop up will display a code that i set, and there is a random number of clicks between say 30 and 50, in the random number between 30 and 50 it creates the pop up to display the code, is that possible?

thanks again.

cuzMazn
12-17-2011, 06:43 AM
that worked a treat :) thanks :) it had been bugging me lol. thanks for all of your help, i have other javascript projects to do, such as integrating php into javascript to creat a forum using iframes that load the pages, from the left iframe into the right hand iframe, i have all the javascript codes and php codes, and as you can tell im no good at javascript, so i needed some help intergrating them :s.

thanks for all your help on this topic. it was a big help, however is there a way to create a random javascipt pop up randomly after the user has gone to a different page say for example, the pop up will display a code that i set, and there is a random number of clicks between say 30 and 50, in the random number between 30 and 50 it creates the pop up to display the code, is that possible?

thanks again.

It's possible, but there are many options of how it works. You simply create a session for that tab/window whenever the page opened, increase the session value by 1 for every click user does, and generate random number of how many clicks for a pop up and boom, pop it up! There are many ways to handle sessions, for example:


Using cookies
Using simple window.variable
Using PHP
Using local storage HTML5
Using iframe to store data
...


Each method has its own advantages and disadvantages, it's up to you which one you should use. If you have no decision, I will make you a demo that works. Those above are just ideas of how to do it.

Tim_

Dan13071992
12-17-2011, 12:13 PM
o right, sounds like something i would look at doing a bit in the future, as of now, im still having problems keeping my sessions :s in php, i have the script below that is included in every script i have:




session_start();
if (isset($_SESSION['uID'])) {
$query=mysql_query("SELECT * FROM users WHERE id=".$_SESSION['uID']);
$user=mysql_fetch_object($query);
} else {
header('Location: index.php');
exit;
}



but it doesnt keep the session very long, and it doesnt save any session data to my /tmp folder, and when i say it doesnt last long, i mean about 8 minutes tops before you refresh and get send straight back to the index page

cuzMazn
12-17-2011, 07:58 PM
o right, sounds like something i would look at doing a bit in the future, as of now, im still having problems keeping my sessions :s in php, i have the script below that is included in every script i have:




session_start();
if (isset($_SESSION['uID'])) {
$query=mysql_query("SELECT * FROM users WHERE id=".$_SESSION['uID']);
$user=mysql_fetch_object($query);
} else {
header('Location: index.php');
exit;
}



but it doesnt keep the session very long, and it doesnt save any session data to my /tmp folder, and when i say it doesnt last long, i mean about 8 minutes tops before you refresh and get send straight back to the index page

Hi dan,

The $_SESSION theoretically should keep your session alive until user closes that tab/window. And for the click problem, you don't even need the PHP sessions, so don't worry about it. Could you solve your random pop up problem yet?

Tim_

Dan13071992
12-17-2011, 08:06 PM
theoretically yes sessions should work like that, however they are not on my site :s even though i have changed the phpini settings.

regarding the pop up window, i think i will leave that for a little while before attempting it, but thanks for your help and support :)

Dan13071992
01-08-2012, 12:15 AM
sorry to bring up an old topic, but i wanted to change this code a little bit, instead of using the alert function in:



$("#responsecontainer").load("inc-mess-jail.php", function(response, status, xhr) {
if (status == "error") {
alert("Error: " + xhr.status + " " + xhr.statusText);
}
});


to a message underneath the


<tr>
<td colspan="2">
<div id="responsecontainer" align="center"></div>
</td>
</tr>


an example is below in a picture, but also it has an "overlib" as i believe it to be called, to show a message that will be in the picture aswell, however it is only when the mouse is over it, aswell as it listing the ammount of times it has tried to reload the div.

http://crimewave.360-tactics.co.uk/errormessage.jpg

Dan13071992
01-11-2012, 03:02 PM
can anyone please help

cuzMazn
01-12-2012, 03:12 AM
sorry to bring up an old topic, but i wanted to change this code a little bit, instead of using the alert function in:



$("#responsecontainer").load("inc-mess-jail.php", function(response, status, xhr) {
if (status == "error") {
alert("Error: " + xhr.status + " " + xhr.statusText);
}
});


to a message underneath the


<tr>
<td colspan="2">
<div id="responsecontainer" align="center"></div>
</td>
</tr>


an example is below in a picture, but also it has an "overlib" as i believe it to be called, to show a message that will be in the picture aswell, however it is only when the mouse is over it, aswell as it listing the ammount of times it has tried to reload the div.

http://crimewave.360-tactics.co.uk/errormessage.jpg

Hi Dan,

If you want to display error(s) to HTML instead of using alert(), replace:



$("#responsecontainer").load("inc-mess-jail.php", function(response, status, xhr) {
if (status == "error") {
alert("Error: " + xhr.status + " " + xhr.statusText);
}
});


By:



$("#responsecontainer").load("inc-mess-jail.php", function(response, status, xhr) {
if (status == "error") {
$(this).html("Error: " + xhr.status + " " + xhr.statusText);
}
});


This just simply replace the function alert() by the function innerHTML() that allows you to change content inside of a div (in this case, text).

I hope it helps,

Tim_

Dan13071992
01-12-2012, 03:19 AM
hi tim, thanks for your help, i was just wondering, what would the div name be in the case of your last post?

also, would that create the error underneath the image? keeping the image there even if there is an error?

cuzMazn
01-22-2012, 08:20 AM
hi tim, thanks for your help, i was just wondering, what would the div name be in the case of your last post?

also, would that create the error underneath the image? keeping the image there even if there is an error?

Hi Dan,

The ID of the div was "responsecontainer". That was why you saw $("#responsecontainer"). All examples I gave were based on your information (div ID, div name...).

If you would like your error message to be placed under the image, you just need to place the <div> under the image. That is.

For a complete code, I will need your source code first.

Good luck.

Tim._

Dan13071992
01-22-2012, 04:10 PM
hi tim, the full source code for the refresh is:



<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(window).ready(function() {
$("#responsecontainer").load("includes/inc-mess-jail.php", function(response, status, xhr) {
if (status == "error") {
$(this).html("Error: " + xhr.status + " " + xhr.statusText);
}
});
var refreshId = setInterval(function() {
$("#responsecontainer").load('includes/inc-mess-jail.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
});
</script>


basically, all i would like is what i have shown before, where if there is an error, the pictures still display, however the error message will come up below it with an "overlib" that if you go mouse over it it will give you a little connection message saying that its trying to re connect. If you read my last couple of posts you will see what i mean as i have displayed pictures.

thanks tim.

Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum