Running .js files Locally

Hey,

I have a file, called “file.js” on my desktop. It contains this:


<script language="JScript">
   var name = "Chris";
   alert(name);
</script>

When I run it, I get this error:


Script:      C:\\WINDOWS\\Desktop\\file.js
Line:        1
Char:        1
Error:        Syntax error
Code:       800A03EA
Source:    Microsoft JScript compilation error

I’ve tried removing the <script> tags. I find that this…

var name =“Chris”;

…produces no error, but doesn’t do anything either. Anytime I try to use the alert() function, I get an error. I’ve tried using both “JScript” and “JavaScript” as the value for the language attribute.

Any ideas?

Change the extension to .html

If I do that, then it will open a browser when I run it. I’m trying to get it to bring up an alert or prompt or something when I execute it. I’m not stupid. :slight_smile: Obviously changing it to a .html file will get the JS to run, just not in the way I want.

Seeing as how it gives me a JScript error message, I’m led to believe that I can get this to work, if I simply find out how Windows wants this thing laid out.

You should be able to use this on the page where you include the js file:

<script language=“JavaScript” src=“file.js”>
</script>

And then make sure the js file itself contains no <script> tags, just pure JavaScript.

If that doesn’t work, then it seems like it has to be something in the js file itself. Although if you’re just setting a var and doing an alert on it, there’s not much that can go wrong.

If it’s still broken, post some code or give us an URL.

I really don’t want to offend anyone here, but it seems that both people who have responded are not really understanding what it is I’m trying to do. I have no problem at all running JavaScript from an HTML file – either embedded, or externally.

What I’m trying to do is allow myself to execute a .js file directly and have that JS process – when I try to “output” anything, IE: print to the screen or call on the alert() function, I get a JScript error (not JavaScript, JScript).

The idea is to have a link to a .js file on my QuickLaunch bar or something. I was hoping to write a JavaScript to provide me with a menu or something – but that’s irrelevant anyway.

As for code/the error: I posted exactly what I got. There really isn’t any more than that.

You must execute a js file through a browser. Unless you have some other special program that noone has written that will run js locally. You have to have something to interpret the js, and for 99% of the population, this is done with their favorite web browser.

I thought the same thing myself originally, but why would I receive a JScript error if this is the case? That implies that I can run something, but that what I ran caused an error.

see if you can get the browser to run it.

I think it is because that alert window is a browser alert window, and if you don’t have a browser open, it isn’t gonna work.

Yes, the browser runs the above JS without problem. I see your point, but getting a JScript error leads me to believe there’s got to be some way to do this.

The fact that it gave me an error must mean I have something somewhere that interprets this. Maybe no one around here knows, but I’d be very surprised if there was not a way to modify the code to work.

Thanks anyway.

If you try to execute a .js file locally, the Windows Scripting Host calls the appropriate scripting engine to handle the script. It interprets js files as JScript, NOT JavaScript. ‘alert’ is not a function in JScript.

You can get an alert to display in JScript, but it involves writing a function to do it, not just simply using ‘alert.’

Below is some JScript for an alert. Put this in your js file and watch the magic happen.


var vbOKCancel = 1;
var vbInformation = 64;
var vbCancel = 2;

var L_Welcome_MsgBox_Message_Text = "This is my message.";
var L_Welcome_MsgBox_Title_Text = "My Alert Box";
Welcome();

function Welcome() {
    var WSHShell = WScript.CreateObject("WScript.Shell");
    var intDoIt;

    intDoIt =  WSHShell.Popup(L_Welcome_MsgBox_Message_Text,
                              0,
                              L_Welcome_MsgBox_Title_Text,
                              vbOKCancel + vbInformation );
    if (intDoIt == vbCancel) {
        WScript.Quit();
    }
}


So if you want to do actions outside of the browser, JScript is an option, but it’s a whole different ball game from JavaScript.

Ah, I see. I had no idea JScript look so much like VBScript. Thanks for your help. :slight_smile: