By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Javascript + InnerHTML problem

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


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

P: n/a

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

P: n/a
"RobG" <rg***@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

P: n/a

Patient Guy wrote:
"RobG" <rg***@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

This discussion thread is closed

Replies have been disabled for this discussion.