ReneGe
asked on
JavaScript: Retry on HTTP GET Connection Timeout
Hi there,
I need to add the feature of if the website is unavailable, to wait 10 seconds and try again. If my website is still unavailable, try again 5 times, if still navailable, echo "UnableToConnect" then exit.
IT WOULD LOGICALLY LOOK SOMETHING LIKE THIS (but in Java Script):
SET ConnectCounter=0
:Home
Echo http://www.MyWebSite.com
Send the GET command to the website and ECHO it out
If can't connect to http://www.MyWebSite.com (
If %ConnectCounter% GEQ 5 (
ECHO UnableToConnect
EXIT
)
SET /a ConnectCounter=%ConnectCou nter%+1
Add a delay of 10Seconds
)
Goto home
HERE IS MY ORIGINAL WORKING JAVA SCRIPT:
var request = new ActiveXObject("Msxml2.XMLH TTP");
var notyetready = 1;
request.onreadystatechange =function( )
{
if(request.readyState==4)
{
WScript.Echo(request.respo nseText);
notyetready = 0;
}
}
var objArgs = WScript.Arguments;
WScript.Echo("http://www.MyWebSite.com");
request.open( "GET", "http://www.MyWebSite.com" , true );
request.send(null);
while( notyetready )
{
WScript.Sleep( 100 );
}
Thanks for your help,
Rene
I need to add the feature of if the website is unavailable, to wait 10 seconds and try again. If my website is still unavailable, try again 5 times, if still navailable, echo "UnableToConnect" then exit.
IT WOULD LOGICALLY LOOK SOMETHING LIKE THIS (but in Java Script):
SET ConnectCounter=0
:Home
Echo http://www.MyWebSite.com
Send the GET command to the website and ECHO it out
If can't connect to http://www.MyWebSite.com (
If %ConnectCounter% GEQ 5 (
ECHO UnableToConnect
EXIT
)
SET /a ConnectCounter=%ConnectCou
Add a delay of 10Seconds
)
Goto home
HERE IS MY ORIGINAL WORKING JAVA SCRIPT:
var request = new ActiveXObject("Msxml2.XMLH
var notyetready = 1;
request.onreadystatechange
{
if(request.readyState==4)
{
WScript.Echo(request.respo
notyetready = 0;
}
}
var objArgs = WScript.Arguments;
WScript.Echo("http://www.MyWebSite.com");
request.open( "GET", "http://www.MyWebSite.com" , true );
request.send(null);
while( notyetready )
{
WScript.Sleep( 100 );
}
Thanks for your help,
Rene
Check this thread : https://www.experts-exchange.com/questions/25460684/Alert-box-when-no-internet-connection-is-present-a-la-facebook.html
For my solution you need to replace the picture https://www.experts-exchange.com/expert/images/logo.jpg with your picture http://www.MyWebSite.com/path/to/a/pic.jpg
ASKER
leakim971,
Thanks for your reference.
I tried to modify my code with yours without success.
Could you please help me with this?
This script is to monitor my website and I used Wikipedia website for the purpose of this post.
Thanks,
Rene
COMMAND LINE FROM MY BATCH FILE TO LAUNCH THE JAVA SCRIPT:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia
JAVA SCRIPT MODIFIED AS PER leakim971's LINK :
var request = new ActiveXObject("Msxml2.XMLH TTP");
var notyetready = 1;
request.onreadystatechange =function( )
{
if(request.readyState==4)
{
WScript.Echo(request.respo nseText);
notyetready = 0;
}
}
var counter = 0;
function timer() {
var objArgs = WScript.Arguments;
WScript.Echo(objArgs(0));
request.open( "GET", objArgs(0) , true );
request.send(null);
setTimeout("timer()", 1000); // try to reload the picture every 1 seconds.
}
function resetCounter() {
counter = 0;
}
function increaseCounter() {
counter++;
// If we fail to reload the pic 5 times, display alert message
if(counter>5) alert("Your browser appears to be offline.\nPlease check your internet connection and try again.");
}
while( notyetready )
{
WScript.Sleep( 100 );
}
Thanks for your reference.
I tried to modify my code with yours without success.
Could you please help me with this?
This script is to monitor my website and I used Wikipedia website for the purpose of this post.
Thanks,
Rene
COMMAND LINE FROM MY BATCH FILE TO LAUNCH THE JAVA SCRIPT:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia
JAVA SCRIPT MODIFIED AS PER leakim971's LINK :
var request = new ActiveXObject("Msxml2.XMLH
var notyetready = 1;
request.onreadystatechange
{
if(request.readyState==4)
{
WScript.Echo(request.respo
notyetready = 0;
}
}
var counter = 0;
function timer() {
var objArgs = WScript.Arguments;
WScript.Echo(objArgs(0));
request.open( "GET", objArgs(0) , true );
request.send(null);
setTimeout("timer()", 1000); // try to reload the picture every 1 seconds.
}
function resetCounter() {
counter = 0;
}
function increaseCounter() {
counter++;
// If we fail to reload the pic 5 times, display alert message
if(counter>5) alert("Your browser appears to be offline.\nPlease check your internet connection and try again.");
}
while( notyetready )
{
WScript.Sleep( 100 );
}
Why replace ? Use the provided code entirely ? Or what does it miss?
ASKER
I tried it but it did not work.
I need to call your script with a batch file, adding as an argument, the website to be monitored, and have the web site dumped in a text file for analysis.
FOR EXAMPLE:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt
Have any ideas?
Thanks,
Rene
I need to call your script with a batch file, adding as an argument, the website to be monitored, and have the web site dumped in a text file for analysis.
FOR EXAMPLE:
cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt
Have any ideas?
Thanks,
Rene
>cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt
Not with javascript : You cannot run javasript from command line.
You need a VBScript
Not with javascript : You cannot run javasript from command line.
You need a VBScript
ASKER
Hmmmmm
When I call the following JavaScript from a cammand line, it works.
Am I missing something here?
COMMAND LINE: cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt
JAVA SCRIPT (NAME: Monitor.js):
var request = new ActiveXObject("Msxml2.XMLH TTP");
var notyetready = 1;
request.onreadystatechange =function( )
{
if(request.readyState==4)
{
WScript.Echo(request.respo nseText);
notyetready = 0;
}
}
var objArgs = WScript.Arguments;
WScript.Echo(objArgs(0));
request.open( "GET", objArgs(0) , true );
request.send(null);
while( notyetready )
{
WScript.Sleep( 100 );
}
When I call the following JavaScript from a cammand line, it works.
Am I missing something here?
COMMAND LINE: cscript Monitor.js http://en.wikipedia.org/wiki/Baloghia>Output.txt
JAVA SCRIPT (NAME: Monitor.js):
var request = new ActiveXObject("Msxml2.XMLH
var notyetready = 1;
request.onreadystatechange
{
if(request.readyState==4)
{
WScript.Echo(request.respo
notyetready = 0;
}
}
var objArgs = WScript.Arguments;
WScript.Echo(objArgs(0));
request.open( "GET", objArgs(0) , true );
request.send(null);
while( notyetready )
{
WScript.Sleep( 100 );
}
WScript is VBScript not javascript. You can run VBScript from command line (with or without cscript) but not javascript.
ASKER
Ok, now I am confised. The script I provided is a WScript, and that is also called VBScript?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
leakim971,
I posted it as WScript and got it done.
Thanks for your help,
Rene
I posted it as WScript and got it done.
Thanks for your help,
Rene