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