ThaJock
10-25-2009, 06:10 PM
I am having trouble with javascript code that is inside an iframe.
I am trying to simulate the w3School's tryit. (http://www.w3schools.com/JS/tryit.asp?filename=try_dom_event_srcelement)
I have tried setting the content of the iframe two different ways and got two different results.
This is example code for what I want the iframe's content to be
<html>
<head>
<script type="text/javascript">
function coordinates(event)
{
x=event.screenX;
y=event.screenY;
alert("X=" x " Y=" y);
}
</script>
</head>
<body onmousedown="coordinates(event)">
<p>
Click somewhere in the document. An alert box will alert the x and y coordinates of the cursor, relative to the screen.
</p>
</body>
</html>
The first way which was successful (but not what i want) is to save the example code as example.html and set the src property of the iframe to that document. Everything works fine including the mouseEvent.
The second way displays the example.html in the iframe as it should but it seems to be ignoring the javascript. This is how I set the iframe content:
I have the code stored in a database table. I use xmlHTTPRequest to grab the code and it is stored in a variable called responseText. From here I the following code sets the iframe content.
r = document.getElementById("iframe");
Doc = r.contentWindow || r.contentDocument;
if(Doc.document){
Doc = Doc.document;
Doc.open();
Doc.write(responseText);
Doc.close();
alert(Doc.documentElement.innerHTML);
}
If anyone can can help me understand why the javascript code doesn't work the second way I would greatly appreciate it.
I am trying to simulate the w3School's tryit. (http://www.w3schools.com/JS/tryit.asp?filename=try_dom_event_srcelement)
I have tried setting the content of the iframe two different ways and got two different results.
This is example code for what I want the iframe's content to be
<html>
<head>
<script type="text/javascript">
function coordinates(event)
{
x=event.screenX;
y=event.screenY;
alert("X=" x " Y=" y);
}
</script>
</head>
<body onmousedown="coordinates(event)">
<p>
Click somewhere in the document. An alert box will alert the x and y coordinates of the cursor, relative to the screen.
</p>
</body>
</html>
The first way which was successful (but not what i want) is to save the example code as example.html and set the src property of the iframe to that document. Everything works fine including the mouseEvent.
The second way displays the example.html in the iframe as it should but it seems to be ignoring the javascript. This is how I set the iframe content:
I have the code stored in a database table. I use xmlHTTPRequest to grab the code and it is stored in a variable called responseText. From here I the following code sets the iframe content.
r = document.getElementById("iframe");
Doc = r.contentWindow || r.contentDocument;
if(Doc.document){
Doc = Doc.document;
Doc.open();
Doc.write(responseText);
Doc.close();
alert(Doc.documentElement.innerHTML);
}
If anyone can can help me understand why the javascript code doesn't work the second way I would greatly appreciate it.