<html>
<head>
<title>paowhpn</title>
<script id="js" type="text/javascript">
function modify(){
document.getElementById('js').innerHTML += "function fun(){ alert( 'hello' ); }";
document.getElementById('r').innerHTML = "<input type='button' value='Press' onclick='fun();' />"
}
</script>
</head>
<body>
<form>
<input type="button" value="Modify" onclick="modify();" />
<br />
<span id="r">
<input type="button" value="Press" />
</span>
<div id="u"></div>
</form>
</body>
</html>
I know the self-modification part works because when I went to "inspect element" in Chrome, it showed that the HTML and Javascript code had indeed been modified. The problem is that it doesn't seem to do anything. The event handler doesn't work, and when I click on the press button, the function fun() doesn't execute.
Is there some technical detail I'm missing here?


Sign In
Create Account


Back to top









