Newbie - DHTML JS not working - Scheme JS??

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
 
R

Richard Cornford

Jim Witte wrote:
<button onclick="replaceWithVar("dog");">Replace
<snip> ^ ^ ^ ^

You are delimiting the onclick attribute string with double quotes, but
using double quotes to define the string literal within it. The HTML
parser is likely taking the first double quote it encounters after the
initial one as ending the string value for the onclick attribute,
resulting in a syntax error in the javascript code provided for the
creation of the event handling method. Passing your HTML through a
validator would probably have flagged that error (by proposing that
the - dog - attribute was invalid).

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.

As presented the first - //obj.appenChild(
(document.createTextNode(str)) ); - statement refers to a global
variable - str - that is probably undefined. Otherwise calling the
createTextNode method inside an appendChild call should work fine.
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..

No, you have messed that up completely:-

var replaced =
(
(document.getElementById("s1")).replaceChild
(
(document.createElement("span")).appendChild
(
(document.createTextNode("str"))
)
)
,
(document.getElementById("obj"))
);

The outer expression is a list, evaluating to the last item in the
list - (document.getElementById("obj")) - and assigning that value to
the - replaced - variable. The first expression in the list is a call to
a replaceChild method that only has one argument, so it should throw a
NOT_FOUND_ERR exception. And the argument to the replace child method
that is provided is the reference to the text Node (the return value
from the appendChild call, the Node appended) that has just been
appended to a SPAN element, not the span element itself. So the text
Node would be removed from the SPAN element and used to replace the
other Node specified in the replaceChild call (if there had been one),
but that would make first appending the text Node to the SPAN pointless.
Has anyone ever though of making a "Scheme" version of JS and
submitting it to the W3C?

Does that statement make any more sense than proposing that someone make
a Java version of C++?

And the W3C wouldn't be interested, as they don't deal with programming
languages. There involvement in this field is in the specifying of
standard object models, which they strive to do in a language neutral
way.
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)
eval?

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)

Before going to far in speculating about how you would like the world to
be, it would be a good idea to actually learn javascript. A strange
truth about javascript is that if you can conceive it you can program it
in javascript.
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?

Zero (You and Dr. Friedman are the only people who can address the first
part of the question, but the W3C won't be interested and the HTML spec
relates to a mark-up language and has nothing to do with
programming/scripting or object models).

Incidentally, have a poke around the javascript material at:-

<URL: http://www.crockford.com/ >

- it may prove enlightening.

Richard.
 
J

Jim Witte

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)
eval?[/QUOTE]

No, I meant something like this:

(define eval-exp
(lambda (expr env)
(match expr
[,b (guard (boolean? b)) b]
[,n (guard (integer? n)) n]
[,sym (guard (symbol? sym)) (apply-env env sym)]
[(,prim ,rands ...) (guard (symbol? prim) (primitive? prim))
(apply-prim prim (map (lambda (e) (eval-exp e env)) rands))]
[(if ,test ,conseq , alt)
(if (eval-exp test env) (eval-exp conseq env) (eval-exp alt env))]
[(let ([,vs ,rands] ...) ,body) [guard (andmap symbol? vs)]
(eval-exp body
;; create an environment
(lambda (id)
(if (memv id vs)
(lookup-var id vs (map (lambda (e) (eval-exp e env)) rands))
(apply-env env id)))
[(letrec ([,fs (lambda ,idss ,bodies ...)] ...) ,body)
(eval-exp body (letrec
([env (lambda (id)
(if (memv id fs)
`(closure ,(lookup-var id idss parems)
,(lookup-var id idss bodies)
,env)
(env id)))])
env))]
[(begin (,E1 ...))
(if (null? (cdr E1)) (eval-exp (car E1) env)
(begin
(eval-exp (car E1) env)
(eval-exp `(begin '(cdr E1)) env)))]
[(lambda (,xs ...) ,body) `(closure ,xs ,body ,env)]
[(,proc ,rands ...) (match p [(closure ,vars ,body ,env)]
(eval-exp body
;; create an environment
(lambda (id)
(if (memv id vs)
(lookup-var id vs (map (lambda (e) (eval-exp e env)) rands))
(apply-env env id)))))]
[(list ,args ...) (list args)])))

which will test with the following call:

(eval-exp '(let ((fact (lambda (fact)
(lambda (n)
(if (zero? n)
1
(* n ((fact fact) (sub1 n))))))))
((fact fact) 6)) (empty-env))

Given that a couple of auxiliary functions are in place.. It isn't 5
lines, but my prof could put it on the board in 5 lines..

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top