I am having an issue with a script which fails when the internet connection is lost, and so it should.
Is there a way that the web page can be automatically refreshed if the connection is lost using JavaScript. I need to continually refresh until the connection is back on.
I know all about META refresh but that does not help.
It depends on what that script is doing. Give us more details.
A JavaScript function needs a certain event to be be fired. Events follow certain actions of the user. Or the losing of the internet connection is not controlled by the user, thus there is no event to be used. You must use somehow that script you are talking about. Tell us more about it.
All the script does is to load a web page which contains data from an SQL database. The script is split into two parts, I have the parent which displays header text and graphics and also has an iFrame which is refreshed ever 30 seconds. The iFrame calls the child which display the data from the database.
Its that simple. The problem I am having is every now and then the internet connection is lost and when this happens the iFrame displays the message "Internet connection lost" or words to that effect.
So I have been trying to come up with a solution in JavaScript that could call the iFrame or the complete page until the connection is live.
Is there a way that the web page can be automatically refreshed if the connection is lost using JavaScript. I need to continually refresh until the connection is back on.
Code:
<script type='text/javascript'>
(function ( img, wait, notify )
{
var iObj = new Image();
iObj.onerror = function()
{
if( notify )
alert( 'reloading' );
location.reload( true );
}
iObj.onload = function()
{
setTimeout( test, wait );
}
function test()
{
iObj.src = img + '?t=' + new Date();
}
test();
})( 'myimage.gif', 2000, true );
// 'myimage.gif' = URL of a small image present on your server
// 2000 = milliseconds between tests
// true/false = display confirmation alert before reload
</script>
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
I must thanks you so much for your time in writing your script.
It appears to work fine. The only issue I have is when the connection is lost I get an alert on screen which requires user action. Is there any way to stop the alert from displaying and just let the script run.
<script type='text/javascript'>
(function ( img, wait, notify )
{
var iObj = new Image();
iObj.onerror = function()
{
if( notify )
alert( 'reloading' );
location.reload( true );
}
iObj.onload = function()
{
setTimeout( test, wait );
}
function test()
{
iObj.src = img + '?t=' + new Date();
}
test();
})( 'images/myimage.gif', 2000, false );
// 'myimage.gif' = URL of a small image present on your server
// 2000 = milliseconds between tests
// true/false = display confirmation alert before reload
With the ( 'images/myimage.gif', 2000, false ); set to false and I run the script in my browser and then disable my connection the whole page stops and IE says "Internet Explorer cannot display the webpage".
If I set it to true I get an alert which requires user action. I think Logic Ali has the right approach but I just need to get a result without an alert popping up.
For my sins I am rubbish at JavaScript, PHP is my bag.
Again, many thanks for any assistance you can provide.
I tried your amended script but when disable the internet connection the iFrame in the web page script falls over and displays the message "Internet Explorer cannot display the webpage".
Do you have any ideas how I can get the iframe to refresh when the connection is restored.
There are now 4 parameters. The second is the ID of the target iframe. Instead of alerting when the last parameter is set to true, messages appear in the title bar instead (document must have a <title> set).
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
Bookmarks