This question is linked with Frustrating innerHTML Issue.
Since this question was a duplicate of another question, so I referred to that question for a solution. I adopted the 2nd answer in the list (with 37 votes). So that I used this coding for dynamically adding javascript to my page.
<html>
<head><title>Javascript Using innerHTML</title>
<script type="text/javascript">
function setcode(code) {
document.write("<img src='empty.gif' onload='function msgbox(msg){alert(msg);};' />");
}
</script>
</head>
<body onload="">
<div id="hello">
</div>
<button onclick='setcode(document.getElementById("hello"))'>SET</button>
<button onclick='msgbox("what up man?!");'>CHECK</button>
</body>
</html>
Now the issue is that when I press the SET button, all my current page is removed out of existence and only the code within document.write() remains. Am I doing something wrong or what? Note that I want to add javascript coding to my page without removing the current visual objects :S