User talk:La Longue Carabine: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎Analyze and Discuss: Added instructions for putting Spidermonkey code on an HTML page.)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Analyze and Discuss==
==Comments and Discussion==
Whatever.
Whatever.



== To Use Spidermonkey Examples in HTML Documents ==
== To Use Spidermonkey Examples in HTML Documents ==


Most of my examples implemented in Spidermonkey can be used on an HTML page as follows:
My JavaScript Spidermonkey examples (those which do not use <code>readline()</code> or read from files, anyway) can be used on an HTML page as follows:


1. Create an HTML page like the one below,
1. Create an HTML page like the one below,
Line 11: Line 10:
2. Paste the Spidermonkey script into it,
2. Paste the Spidermonkey script into it,


3. Comment out the call to <code>main()</code>,
3. Comment out the hash-bang line,


4. Comment out the call to <code>main()</code>,
4. Load into your browser and enjoy!

5. Load into your browser and enjoy!


<pre>
<pre>
Line 22: Line 23:
var printBuf = '';
var printBuf = '';
function putstr() {
function putstr() {
var pad = arguments.length &gt; 1 ? ' ' : ''
for (var i = 0; i &lt; arguments.length; i++) {
for (var i = 0; i &lt; arguments.length; i++) {
printBuf += arguments[i];
printBuf += arguments[i] + pad;
if (arguments.length &gt; 1) printBuf += ' ';
}
}
}
}


function print() {
function print() {
var pad = arguments.length &gt; 1 ? ' ' : ''
for (var i = 0; i &lt; arguments.length; i++) {
for (var i = 0; i &lt; arguments.length; i++) {
putstr(arguments[i]);
putstr(arguments[i] + pad);
if (arguments[i].length &gt; 1) putstr(' ');
}
}
var d = document.createElement('div');
var d = document.createElement('div');
Line 39: Line 40:
}
}


// Put my code here, but remove the call to main
// Put my code here, but remove the hash-bang and call to main
// #!/usr/bin/env js
// that is usually the last line of the script.

// ...

//main()
//main()



Latest revision as of 16:22, 23 July 2012

Comments and Discussion

Whatever.

To Use Spidermonkey Examples in HTML Documents

My JavaScript Spidermonkey examples (those which do not use readline() or read from files, anyway) can be used on an HTML page as follows:

1. Create an HTML page like the one below,

2. Paste the Spidermonkey script into it,

3. Comment out the hash-bang line,

4. Comment out the call to main(),

5. Load into your browser and enjoy!

<html>
<head>
<script>
var outElmId = 'output';
var printBuf = '';
function putstr() {
    var pad = arguments.length > 1 ? ' ' : ''
    for (var i = 0; i < arguments.length; i++) {
        printBuf += arguments[i] + pad;
    }
}

function print() {
    var pad = arguments.length > 1 ? ' ' : ''
    for (var i = 0; i < arguments.length; i++) {
        putstr(arguments[i] + pad);
    }
    var d = document.createElement('div');
    d.innerHTML = printBuf;
    document.getElementById(outElmId).appendChild(d);
    printBuf = '';
}

// Put my code here, but remove the hash-bang and call to main
// #!/usr/bin/env js

// ...

//main()

</script>
</head>
<body onload="main()">
<pre id="output"></pre>
</body>
</html>