5

I just want to play a little bit with JS. I would like to use JS without HTML or a HTML page -- just a console for output. Whats the best way? I have this:

<html>
<body>
<h1>Playin around</h1>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

script.js:

var test = "global";

function out() {
    var test = "local";
    return test;
}

window.alert(out());

How can I replace window.alert so I can get a console output & how to make a "main" or something, that I don't have to start on the HTML?

Thank you

5 Answers 5

4

I use JS Bin and JS Fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

How to do it in Eclipse?
2

I'll be going off topic here. If you want something easy and quick to test javascript code, here's your path (based on my experience):

  1. Firebug in Firefox
  2. Javascript Console in Chrome
  3. JSFiddle.net

Before you asked, I don't know how to do javascript code in eclipse, sorry. *you may vote down this answer).

2 Comments

Dead link for option 2
Yes, JSFiddle.net is good one. But if I want to use Eclipse how can I do it?
2

I use Windows primarily.

In every copy of Windows since Windows 95, there's a script host that allows you to run the Microsoft variant of Javascript, known as JScript, from the command line. Open a cmd.exe window, and type in filename.js and Windows will run the Jscript in that file.

JScript is Javascript, with some extensions for Windows. One extension: a JScript program can create a COM object; this means you could use a JScript program to automate Word, or to create a "Compressed folder" (a zip file), or to send a FAX, by interacting with the COM objects available on Windows for those purposes.

But, if you want to just focus on the Javascript language, how to structure programs and modules, or more pure algorithmic programming, then JScript running in Windows Script Host is a really good option, if you are on Windows.

For example, the JSLINT, JSHINT, and CSSHINT tools - all written in pure Javascript - are all available in versions that run on Windows Script Host, from the Windows command line. Using those versions, you could include a LINT check into a build script.

If you run a Javascript program on WSH, there is no HTML DOM. There is no window object, no document object. It's just Javascript.

Here's how I would structure your simple program for use on WSH:

(function(globalScope){

    var test = "global"; 

    function say(x){ WScript.Echo(x); }

    function out() { 
        var test = "local"; 
        return test; 
    } 

    say(out()); 

}(this));

Notice the use of WScript.Echo - that's a JScript-only extension. This is the output of the program:

enter image description here

For something a little more interesting, given this JS module:

(function(globalScope){
    'use strict';

    function say(x){ WScript.Echo(x); }

    if (typeof Array.prototype.numericSort !== 'function') {
        Array.prototype.numericSort = function() {
            return this.sort(function(a,b){return a - b;});
        };
    }

    var list = [17,  23, 2003, 39, 9172, 414, 3];

    say("array: [" + list.toString() + "]");

    var sortedList = list.numericSort();

    say("sorted: [" + sortedList.toString() + "]");

}(this));

Running it on WSH looks like this:

enter image description here

You can see I've used prototypal inheritance of the Array object. All the usual JS language features are there. You can learn quite a bit, just doing programs that run from the command line.

Comments

0

Simplest solution would be to install FireBug for Firefox, then use the console:

console.log(out());

2 Comments

and if I would like to have an output only in eclipse (without firebug, firefox and other stuff) ?
I don't know. I don't use Eclipse enough to even know if it has a JS engine built in.
0

You want to run the javascript with out html page, Press F12 in Chrome then goto console and paste your code then press enter

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.