Arturio
Arturio

Reputation: 466

How to automatically close webpage using javascript?

I have found many questions like mine in different forums, but I couldn't find an answer that actually helps to solve my problem in any of them.

Basically, what I want is to open an URL through command prompt, it will open the browser, processes a webpage, and then I would like it to automatically close.

Why do I need this? I have an application that runs on IIS. There are some routines I need to run everyday in my application. I can simply kick of these routines by running an URL similar to the showed below:

http://myapplication.com/DoStuff.aspx?

The Problem is that this is totally manual. I was wondering if I could create a batch file calling my URL "start http://myapplication.com/DoStuff.aspx?", and then I could create a task on Windows to run that batch file everyday. That works for me except that the browser will not close automatically. What I mean is, I could try it, but at the end of a week, I would have at least 5 windows opened.

What I have tried:

I have tried to solve it by using javascript, but I always end up getting this message:

scripts may close only the windows that were opened by it

It does not matter the javascript function I create using "window.close()", the windows won't close.

Chrome and Firefox returns that message. IE let's me try to close the window, but it asks in a popUp if I really want to close it.

Upvotes: 2

Views: 1333

Answers (3)

blaze_125
blaze_125

Reputation: 2317

Using batch files only because I strongly believe you do not need JavaScript to do this.

::start a new browser session at the given url
start iexplore "http://www.google.com"
::wait for whatever process to end if you actually have to wait
timeout 15
::kill the browser process
taskkill /im iexplore.exe /f /t

If it runs on a machine nobody interacts with, and you know the session you log into will be the last session you got out of, then you know the session you're getting into already has IE open. So you could reverse the order of the script and not care much about timing

::kill the browser already opened
taskkill /im iexplore.exe /f /t
::open a new browser session at the given url
start iexplore "http://www.google.com"
::if you have to wait, but don't know how long... leave the browser window open. We'll close it next time we run this batch file anyway.

Upvotes: 1

guest271314
guest271314

Reputation: 1

At *nix you can create a bash script containing

#!/bin/bash

/usr/bin/chromium-browser --user-data-dir="/home/USER/.config/chromium-no-flags" "http://myapplication.com/DoStuff.aspx"

within DoStuff.aspx, use setTimeout() or other function to call window.close() when task is complete

setTimeout(close, 10000)

or

new Promise((resolve, reject) => {
  // do asynchronous stuff
  resolve(/* value */)
})
.then(close)
.catch(close)

For *indows equivalent of cron see

See also

Upvotes: 0

Badacadabra
Badacadabra

Reputation: 8507

What you need is probably a headless browser like PhantomJS (WebKit browser without GUI). I would recommend you to use CasperJS to create scripts even more easily...

Install Phantom and Casper globally on your system and write a minimal automation script like so:

var casper = require('casper').create();

casper.start('http://myapplication.com/DoStuff.aspx');

casper.then(function() {
  // Do something here...
});

casper.run();

Set a cron job (or Windows equivalent) to execute the script with the casperjs bin. Normally, it should do the trick...

Upvotes: 1

Related Questions