363,924 Members | 2603 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Javascript + InnerHTML problem

Henry
P: n/a
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.

Aug 9 '06 #1
Share this Question
Share on Google+
4 Replies


Henry
P: n/a
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.

Aug 9 '06 #2

RobG
P: n/a
RobG

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



--
Rob

Aug 9 '06 #3

Patient Guy
P: n/a
Patient Guy
"RobG" <rgqld@iinet.net.auwrote in comp.lang.javascript:

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.



Aug 9 '06 #4

RobG
P: n/a
RobG

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

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

Aug 9 '06 #5

Post your reply

Help answer this question



Didn't find the answer to your JavaScript / Ajax / DHTML question?

You can also browse similar questions: JavaScript / Ajax / DHTML