J
Jim Witte
I've got the following little example of a span replacement via JS:
<html>
<head>
<title>Example of DHTML</title>
<body>
<p id="s1"> The thug kicked the <span id="obj">man</span>.</p>
<script type="text/javascript">
function replaceTable(){
var obj = document.createElement("span");
var text = document.createTextNode("table");
obj.appendChild(text);
//obj.appenChild( (document.createTextNode(str)) );
var para = document.getElementById("s1");
var oldObj = document.getElementById("obj");
var replaced = para.replaceChild(obj, oldObj)
}
function replaceWithVar(str){
var obj = document.createElement("span");
var text = document.createTextNode(str);
obj.appendChild(text);
//obj.appenChild( (document.createTextNode(str)) );
var para = document.getElementById("s1");
var oldObj = document.getElementById("obj");
var replaced = para.replaceChild(obj, oldObj)
}
</script>
<button onclick="replaceTable();">Replace object with table</button>
<br>
<button onclick="replaceWithVar("dog");">Replace object with
'dog'</button>
<button onclick="replaceWithVar("cat");">Replace object with
'cat'</button>
<button onclick="replaceWithVar("man");">Replace object with
'man'</button>
</body>
</html>
My first question concerns the two functions: The first replace
function (without any paramet) works. The second function, which is
supposed to to take string as a parameter, does not (it doens't do
anything when any of the other three buttons are clicked). Is there
something wrong with my syntax, or can this not be done with JS? (I'm
using a recent build of Camino for MacOSX - based off of Mozilla Gecko,
so it should be pretty standards compliant)
My second question concerns the commented line in the functions. I
though I could save a line by putting the "text" variable definition
inside the appendChild function. But if I switch the commenting to the
above line (obj.appenChild(text)), it doesn't work either. Can this not
be done either? (It would be really neat (if convoluted) if I could also
do that with an l-value, so I could right
var replaced = ((document.getElementById("s1")).replaceChild(
(document.createElement("span")).appendChild(
(document.createTextNode("str")))),
(document.getElementById("obj")))
I think I got that right..
Has anyone ever though of making a "Scheme" version of JS and
submitting it to the W3C? I can see *all* sorts of possibilities here -
(and for malware unfortunately) - starting with the idea of writing full
interpreters in 5 lines (if it were a full working Scheme interpretation
that is) But given the hierarchical nature of HTML, treating it as a
list and letting you use recursion, car, and cdr, on it seems only
natural.
The fact that the different element objects are typed presents a small
problem - you couldn't write your list code as simply as you could before
without some associated functions to parse it, as an object couldn't be
represented just as an atom, but as a pair (dotted or not - I'd prefer
proper lists, as they are eaiser to deal with, and extendable), so an
element would be ('span <id identifier if needed> (<value of spam as a
list))
So the above I *think* would be
('document ('element 's1 ('span 'obj ('text "man"))))
This assumes that document and text elements don't have identifiers.
Automatic coercian between quoted identifiers and text strings would have
to be done to make things easier, and allow certain names for things that
aren't allowed as Scheme identifiers (things with spaces for one)
Any chance that I could write this up as a proposal with Dr. Friedman
(Indiana University Bloomington), and submit it to the W3C with any
reasonable chance that it would get into the next HTML spec or so?
Jim Witte
<html>
<head>
<title>Example of DHTML</title>
<body>
<p id="s1"> The thug kicked the <span id="obj">man</span>.</p>
<script type="text/javascript">
function replaceTable(){
var obj = document.createElement("span");
var text = document.createTextNode("table");
obj.appendChild(text);
//obj.appenChild( (document.createTextNode(str)) );
var para = document.getElementById("s1");
var oldObj = document.getElementById("obj");
var replaced = para.replaceChild(obj, oldObj)
}
function replaceWithVar(str){
var obj = document.createElement("span");
var text = document.createTextNode(str);
obj.appendChild(text);
//obj.appenChild( (document.createTextNode(str)) );
var para = document.getElementById("s1");
var oldObj = document.getElementById("obj");
var replaced = para.replaceChild(obj, oldObj)
}
</script>
<button onclick="replaceTable();">Replace object with table</button>
<br>
<button onclick="replaceWithVar("dog");">Replace object with
'dog'</button>
<button onclick="replaceWithVar("cat");">Replace object with
'cat'</button>
<button onclick="replaceWithVar("man");">Replace object with
'man'</button>
</body>
</html>
My first question concerns the two functions: The first replace
function (without any paramet) works. The second function, which is
supposed to to take string as a parameter, does not (it doens't do
anything when any of the other three buttons are clicked). Is there
something wrong with my syntax, or can this not be done with JS? (I'm
using a recent build of Camino for MacOSX - based off of Mozilla Gecko,
so it should be pretty standards compliant)
My second question concerns the commented line in the functions. I
though I could save a line by putting the "text" variable definition
inside the appendChild function. But if I switch the commenting to the
above line (obj.appenChild(text)), it doesn't work either. Can this not
be done either? (It would be really neat (if convoluted) if I could also
do that with an l-value, so I could right
var replaced = ((document.getElementById("s1")).replaceChild(
(document.createElement("span")).appendChild(
(document.createTextNode("str")))),
(document.getElementById("obj")))
I think I got that right..
Has anyone ever though of making a "Scheme" version of JS and
submitting it to the W3C? I can see *all* sorts of possibilities here -
(and for malware unfortunately) - starting with the idea of writing full
interpreters in 5 lines (if it were a full working Scheme interpretation
that is) But given the hierarchical nature of HTML, treating it as a
list and letting you use recursion, car, and cdr, on it seems only
natural.
The fact that the different element objects are typed presents a small
problem - you couldn't write your list code as simply as you could before
without some associated functions to parse it, as an object couldn't be
represented just as an atom, but as a pair (dotted or not - I'd prefer
proper lists, as they are eaiser to deal with, and extendable), so an
element would be ('span <id identifier if needed> (<value of spam as a
list))
So the above I *think* would be
('document ('element 's1 ('span 'obj ('text "man"))))
This assumes that document and text elements don't have identifiers.
Automatic coercian between quoted identifiers and text strings would have
to be done to make things easier, and allow certain names for things that
aren't allowed as Scheme identifiers (things with spaces for one)
Any chance that I could write this up as a proposal with Dr. Friedman
(Indiana University Bloomington), and submit it to the W3C with any
reasonable chance that it would get into the next HTML spec or so?
Jim Witte