Javascript redirect when device has restarted

less than 1 minute read

Over the past few months I have been building a sensor device with a embedded webserver for configuration and reporting. When a user saves a configuration to the device the device needs to be restarted before the changes come in to effect.

While the device is restarting the webserver is unreachable and the users get a blank page or 404 pages, this is bad for the users. So I created this simple script to check to see if the device has been restarted correctly then redirect the users to the correct page. This script uses JQuery

Script: Redirect on reboot

<script type="text/javascript">
		function UrlExists(url) {
			var http = new XMLHttpRequest();
			http.open('HEAD', url, false);
			http.send();
			return http.status!=404;
		}

		var checkCount = 0 ; 

		function CheckServerUp(){
			checkCount++; 
			$('#status').text('Checking. ' + checkCount ) ;			
			if( checkCount > 30 ) {
				$('#Error').html('The device is taking a long time to reboot, <a href="/refresh2.htm">Click here to continue</a>' + checkCount ) ;
			}

			if( UrlExists( "refresh2.htm" ) ) {
				$('#status').text('Device running, redirecting...') ; 
				window.location.replace( "/refresh2.htm" );				
			}
		};

		$(document).ready(function(){
			setInterval( "CheckServerUp()", 1000 );			
		});
	</script>

 

Leave a comment