Connecting Tech Pros Worldwide Forums | Help | Site Map

Javascript + InnerHTML problem

Henry
Guest
 
Posts: n/a
#1: Aug 9 '06
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.


Henry
Guest
 
Posts: n/a
#2: Aug 9 '06

re: Javascript + InnerHTML problem


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.

RobG
Guest
 
Posts: n/a
#3: Aug 9 '06

re: Javascript + InnerHTML problem



Henry wrote:
Quote:
Hi
>
I have a problem, pls see below:
>
<html>
:
<script type="text/javascript">
<!--
Don't use HTML comments inside script elements.

Quote:
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.

Quote:
// -->
</script>
>
:
<center id="emptySpace"></center>
The center element is deprecated in HTML 4.

Quote:
<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();">



--
Rob

Patient Guy
Guest
 
Posts: n/a
#4: Aug 9 '06

re: Javascript + InnerHTML problem


"RobG" <rgqld@iinet.net.auwrote in comp.lang.javascript:

Quote:
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.



RobG
Guest
 
Posts: n/a
#5: Aug 9 '06

re: Javascript + InnerHTML problem



Patient Guy wrote:
Quote:
"RobG" <rgqld@iinet.net.auwrote in comp.lang.javascript:
>
>
Quote:
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?
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.

Quote:
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.


--
Rob

Closed Thread