Click here to Skip to main content
15,075,944 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
if "Webpage is not available" or "Unable to connect"
then browser auto relaod/refresh.

i'm already try using plugin or javascript but still not working properly.
Posted
Comments
jgakenhe 24-Jan-16 13:35pm
   
Here is how to check if web page is connected in JavaScript: http://www.kirupa.com/html5/check_if_internet_connection_exists_in_javascript.htm. This is well explained and you should take a look.


//I added the location.reload() if connection fails.
function doesConnectionExist() {
var xhr = new XMLHttpRequest();
var file = "http://www.yoursite.com/somefile.png";
var randomNum = Math.round(Math.random() * 10000);

xhr.open('HEAD', file + "?rand=" + randomNum, false);

try
{
hr.send();

if (xhr.status >= 200 && xhr.status < 304)
{
return true;
}
else
{
return false;
location.reload();
}
}
catch (e)
{
return false;
}
}
JaironLanda 26-Jan-16 22:53pm
   
Thanks!
jgakenhe 27-Jan-16 0:08am
   
No problem. I moved this to solution so Code Project can complete this thread.
Sergey Alexandrovich Kryukov 24-Jan-16 13:47pm
   
Why? If a page is really not available, not due to some intermittent connection failure, but is really not available, do you want to hang your browser in repeated attempts to load the page? :-)
—SA

This has to be a plugin for the browser since, well, if the page doesn't load, neither does your javascript to automatically refresh the page if not available.

You'd have to write a plugin for each browser you want to support.
   
Here is how to check if web page is connected in JavaScript: http://www.kirupa.com/html5/check_if_internet_connection_exists_in_javascript.htm. This is well explained and you should take a look.


C#
//I added the location.reload() if connection fails.
function doesConnectionExist() 
{
	var xhr = new XMLHttpRequest();
	var file = "http://www.yoursite.com/somefile.png";
	var randomNum = Math.round(Math.random() * 10000);

	xhr.open('HEAD', file + "?rand=" + randomNum, false);

	try
	{
		hr.send();

		if (xhr.status >= 200 && xhr.status < 304)
		{
			return true;
		}
		else
		{
			return false;
			location.reload();
		}
	}
	catch (e)
	{
		return false;
	}
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900