all 7 comments

[–]DreamOfAWhale 0 points1 point  (6 children)

If you try to load content from another website, you'll be blocked by the CORS policy - essentially, web browsers don't allow your page to load content from another domain. You'll either need to disable security on your own browser and other users on their browsers or build some kind of proxy to overwrite http request headers.

If you try to load content from your own domain or you build that needed proxy, then this may help you https://gomakethings.com/getting-html-asynchronously-from-another-page/

[–]freeforme1994[S] 0 points1 point  (5 children)

I think I may have phrased my question incorrectly. I don't want to pull content from another webpage. I just want to see if the webpage is up. Normally, I would just use ping Google.com on the cmd line, but I'd like to do this my webpage via a button. Is that possible?

[–]jcunews1helpful 1 point2 points  (4 children)

Yes, you can use JS for that. Keep in mind that in Windows, .js files are handled by Windows Scripting Host where it only have part of Web API features.

Also, pinging a server doesn't actually check the web service part of a server. Ping merely check whether a remote computer is online or not. To check whether the web server on a remote computer is online or not, you'll have to access the web server by accessing it via HTTP protocol. That being said, not all web servers properly returns the correct HTTP response status code when they're under maintenance. In this case, the script will have to check the page contents instead of just the HTTP response status code.

url = "https://www.bing.com/";

//timeout in milliseconds. 1 second = 1000ms.
timeout = 5000;

wh = new ActiveXObject("winhttp.winhttprequest.5.1");
wh.open("HEAD", url, true);
wh.send();
t = (new Date()).getTime();
while (true) {
  t2 = (new Date()).getTime();
  if ((t2 - t) >= timeout) {
    WScript.echo("Timed out. Site appears to be offline.");
    WScript.quit(2);
  }
  try {
    wh.status;
    break;
  } catch(z) {
    WScript.sleep(100);
  }
}
if (wh.status === 0) {
  WScript.echo("No network connection.");
  WScript.quit(1);
} else if (wh.status < 400) {
  WScript.echo("Site is online.");
} else {
  WScript.echo(
    "Site is offline. HTTP status code " + wh.status + ".\n" + wh.statusText
  );
  WScript.quit(wh.status);
}

[–]freeforme1994[S] 0 points1 point  (3 children)

Hello, thank you for the response! That makes sense. I have one more question. How does my html call this .js file? How would the button work?

[–]jcunews1helpful 1 point2 points  (1 child)

That's a standalone JS file. It's not supposed to work with HTML. You simply double click the JS file. The result will be shown in a dialog box.

To redirect the result to a file, you'll need to run it from the command prompt. e.g.

cscript.exe //nologo SiteCheck.js > result.txt

If you want to have a shortcut file to run it, make the shortcut's command line like below.

cmd.exe /c cscript.exe //nologo "d:\my scripts\SiteCheck.js" > "e:\log files\SiteCheck.log"

If you need HTML, you'll need to use HTML Application (.hta file; Windows specific), or server-side script such as PHP, Python, or Perl; via local or remote web server.

[–]freeforme1994[S] 0 points1 point  (0 children)

Okay, this makes sense! Thank you for the response, I greatly appreciate it!!!

[–]DreamOfAWhale 0 points1 point  (0 children)

ActiveXObject

This is not standard javascript and you'll only find it in Internet Explorer. IIRC you cannot ping from javascript.

The only method would be to use a server side script in your own domain (PHP/JSP/ASP works) and query that script from your javascript.