Javascript + InnerHTML problem

H

Henry

Hi

I have a problem, pls see below:

<html>
:
<script type="text/javascript">
<!--
function addCode () {
document.getElementById('emptySpace').innerHTML+=
"<b>This is a test part </b><script
language='javascript'>window.alert('Hi');</script>";
}
// -->
</script>

:
<center id="emptySpace"></center>
<a href="javascript:addCode()">Add Code</a>
:
</html>

The problem is when I clicked on the link, only the text "This is a
test part" was displayed. The alert box was not appear.

Please help me, thanks.
 
H

Henry

Hi all,

i changed

<script language='javascript'>window.alert('Hi');</script>

to

<script defer>window.alert('Hi');</script>

and it works ....

but only in IE but not in Mozilla Firefox....

Please help me...Thanks.
 
R

RobG

Henry said:
Hi

I have a problem, pls see below:

<html>
:
<script type="text/javascript">
<!--

Don't use HTML comments inside script elements.

function addCode () {
document.getElementById('emptySpace').innerHTML+=
"<b>This is a test part </b><script
language='javascript'>window.alert('Hi');</script>";
}

Don't allow posted code to auto-wrap, wrap it manually at about 70
characters. Inserting script elements using innerHTML is problematic
since there is no public standard on how it should behave.

innerHTML is a proprietary Microsoft invention that has been widely but
inconsistently copied - you shouldn't rely on it.

// -->
</script>

:
<center id="emptySpace"></center>

The center element is deprecated in HTML 4.

<a href="javascript:addCode()">Add Code</a>
:
</html>

The problem is when I clicked on the link, only the text "This is a
test part" was displayed. The alert box was not appear.

Put the script in an external file. Create the script element using
document.createElement then assign an src attribute with a value that
points to the external file. There are lots of examples in the
archives.

file: a.js
==========

alert('hi');

file: a.html
============

<title>load script test</title>
<script type="text/javascript">

function loadScript(){
var oScr = document.createElement('script');
oScr.type = 'text/javascript';
oScr.src = 'a.js';
document.body.appendChild(oScr);
}

</script>
<input type="button" value="Add script" onclick="loadScript();">
 
P

Patient Guy

innerHTML is a proprietary Microsoft invention that has been widely but
inconsistently copied - you shouldn't rely on it.


I agree. So how does one convert a string of HTML to a DOM document
fragment for appending?

I have my own code, but I'll bet it's bug-ridden.
 
R

RobG

Patient said:
I agree. So how does one convert a string of HTML to a DOM document
fragment for appending?

The obvious answer is don't design your application to use HTML
fragments. :)

It is probablly OK to inject HTML where you are getting it from a
server (e.g. AJAX) in small chunks that you know work, however it
doesn't make much sense to use it for script elements, where logic
dictates the types of elements you are creating (or their attributes)
or where you know it is inconsistent across browsers.

IE is quite slow using DOM methods compared to innerHTML, but Firefox
isn't. I expect IE 7 to be much more efficient.

I have my own code, but I'll bet it's bug-ridden.

Check out the DOM 3 Load and Save spec, though at present IE lacks
useful support (again, IE 7 may change that but I don't think it will)
and Firefox's support is limited.
 

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
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top