javascript generated html

Q

quikquic

Suppose I download a html file with javascript in it, for example,

<html>
<body>
<script language="JavaScript">
document.write("Hello");
</script>
</body>
</html>

How can I translate that to pure html,

<html>
<body>
Hello
</body>
</html>

I am looking for a c++ solution.

Thanks,

qk
 
L

Lasse Reichstein Nielsen

Suppose I download a html file with javascript in it, for example, ....
<script language="JavaScript">
document.write("Hello");
</script> ....
How can I translate that to pure html, ....
Hello
....

In this, simple, case it is fairly easy. Remove the script tags and
change calls to document.write into its value. But that only works when
all the script element contains are calls to document.write.

If the Javascript is more complicated, you will have to execute it to
know what it does. The simplest solution would be to load a browser
and put the page into it, and then extract the generated HTML.
Otherwise, you will need an implementation of Javscript and a runtime
environment similar ot that provided by a browser in order to execute
the code correctly.

If you are aiming for the Windows platform, You can probably remotely
control IE, or make an instance of the HTML rendering widget, and put
the code into it. When (or if) it finishes parsing the page, including
the embedded Javascript, you can use
document.documentElement.innerHTML to get the contents of the
resulting page's html element.

If you are using this for anything critical, and the user supplies the
page, then they can make pages where the Javascript never finishes
executing. A simple "while(true){}" can do it, but you can check in
advance whether a script will loop forever or not, it can always be
disguised to fool your check. You will have to time-out the browser
if it runs for too long.

You will also be open to any security bugs in IE.
<URL:http://www.pivx.com/larholm/unpatched/>

/L
 
J

Jerry Coffin

Suppose I download a html file with javascript in it, for example,

<html>
<body>
<script language="JavaScript">
document.write("Hello");
</script>
</body>
</html>

How can I translate that to pure html,

In any more than a trivial case like this, you can't -- the primary
reason people use Javascript in the first place is to do things that are
impossible or thoroughly unreasonable using HTML.
 
D

DU

Suppose I download a html file with javascript in it, for example,

<html>
<body>
<script language="JavaScript">
document.write("Hello");
</script>
</body>
</html>

How can I translate that to pure html,

<html>
<body>
Hello
</body>
</html>

I am looking for a c++ solution.

Thanks,

qk

IMO, your question with your specific example is easy to answer: just
remove the script tags. But the answer to the general question is it is
impossible as there are many many ways (DOM methods, even DOM
attributes) to dynamically add (and/or insert and/or append, modify,
move, split, etc.. even import) new elements (and/or new text node
values) into an html file. Adding new DOM nodes with display:none or
visibility:hidden or opacity to 0 or etc... (dynamically settable later)
would still have to be considered as cases to consider.

Your question is a very valid one though as W3C thinks people are
resorting to (and/or over-using, mis-using, abusing) DHTML/javascript
way too often when simple, normal HTML would suffice.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
I

Ivo

Suppose I download a html file with javascript in it, for example,

<html>
<body>
<script language="JavaScript">
document.write("Hello");
</script>
</body>
</html>

How can I translate that to pure html,
I am looking for a c++ solution.
Thanks,
qk
You need a very clever script that interprets the javascript, because
it has to know in advance what is going to be asked of the javascript.
Onclicks, onchanges and all other events will have to be assumed. In
other words, I don't think it 's really possible.

Why was it written in javascript in the first place??

If you know that the javascript doesn't change much, but it is still
too much work to convert all your pages by hand, perhaps you could run
them as they are, and even add a little javascript that submits the
generated source back to the server once it has finished generating,
where you can replace the page with the received html code.
HTH
Ivo
 
Q

quikquic

How does the browser understand javascript? Does the browser transfer
it into html (if it is something like document.write) first?

Thanks,
 
J

Jerry Coffin

How does the browser understand javascript? Does the browser transfer
it into html (if it is something like document.write) first?

No -- the browser has a Javascript interpreter (or invokes a separate
one) that understands Javascript. HTML has only the VERY most limited
involvement -- one of the two (usually the browser, I'd guess) has to
recognize the bit of HTML that marks the end of the Javascript, so
things switch back to normal HTML mode at the end. What the Javascript
produces does NOT (in any case of which I'm aware at any rate) get
converted to HTML to be displayed or anything like that though.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
seen in news:comp.lang.javascript said:
No -- the browser has a Javascript interpreter (or invokes a separate
one) that understands Javascript. HTML has only the VERY most limited
involvement -- one of the two (usually the browser, I'd guess) has to
recognize the bit of HTML that marks the end of the Javascript, so
things switch back to normal HTML mode at the end. What the Javascript
produces does NOT (in any case of which I'm aware at any rate) get
converted to HTML to be displayed or anything like that though.

That which is written by document.write, and that which is written by
DynWrite (FAQ 4.15), is treated as HTML. For example, <br> does not
make four marks on the page, but causes the insertion point to move to a
new line.
 

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,093
Messages
2,570,607
Members
47,226
Latest member
uatas12

Latest Threads

Top