Rory A Campbell
Rory A Campbell

Reputation: 131

How do I run a javascript within html from the command line and include arguments?

I have an html javascript program that opens 4 browser windows. I want to run this from the command line and include arguments that specify the URLs to be opened. Is this possible?

The current code looks like this:

<html>
<script>
function openWindows(url1, url2, url3, url4) {
  window1=window.open(url1,'','width=725,height=480');
  window1.moveTo(0, 0);
  window2=window.open(url2,'','width=725,height=480');
  window2.moveTo(0, 480);
  window3=window.open(url3,'','width=725,height=480');
  window3.moveTo(725, 0);
  window4=window.open(url3,'','width=725,height=480');
  window4.moveTo(725, 480);
};

</script>
<body onload="openWindows()">
</html>

How can I modify it so that it can be run with URL arguments form the command line? Im using a macbook. Thanks

Upvotes: 2

Views: 2850

Answers (1)

JosephGarrone
JosephGarrone

Reputation: 4161

It is not possible (At least in practicality) to interface from your MacBook Terminal/Command Prompt window and a Javascript webpage.

I would recommend that you look at other methods to try and accomplish this, such as an Apple Script which can open up your browser and navigate to 4 separate URLs.

However, if you must do it in Javascript, and you must be able to send these commands from your terminal, I would look into having your terminal accept 4 URL inputs, and then write the URLs to a Javascript file.

>> Input URL 1: ...
>> Input URL 2: ...
>> Input URL 3: ...
>> Input URL 4: ...
>> Now that you have the 4 urls, write to a Javascript file in the format:
var url1 = inputfromurl1;
var url2 = inputformurl2; //And so on for 4 urls
function openWindows()
{
    window1=window.open(url1,'','width=725,height=480');
    window1.moveTo(0, 0);
    window2=window.open(url2,'','width=725,height=480');
    window2.moveTo(0, 480);
    window3=window.open(url3,'','width=725,height=480');
    window3.moveTo(725, 0);
    window4=window.open(url3,'','width=725,height=480');
    window4.moveTo(725, 480);
};

Then I would save that file as browserscript.js or something similar. I would then have a HTML file like such:

<html>
<head>
    <script src="browserscript.js"/>
</head>
<body onload="openWindows();">
</body>

Upvotes: 2

Related Questions