I have a popup window that must span multiple frames, so I’m using window.createPopup to get this to work. (IE6, 7, and 8.)
Below is the function I’m using to create the popup:
function ShowMyPopup() {
notificationPopup = window.createPopup();
$(notificationPopup.document.body).load("/notification.html");
notificationPopup.show($(sourceFrame.document.body).width() - 510, $(sourceFrame.document.body).height() - (510 - $(sourceFrame.document.body).height()), 500, 500, sourceFrame.document.body);
}
This seems to work pretty well. I see the popup window as I should. The problem is, no matter what I do, I can’t seem to access any of the DOM elements in the resulting popup window. I’ve tried various jQuery methods as well as a straight up getElementById, and all return NULL. Below is the contents of notification.html:
<html>
<head>
<script type="text/javascript">
$(document).ready(function () {
alert($(document).html());
alert($("#divNotification").html());
alert(document.getElementById("divNotification"));
});
</script>
</head>
<body>
<div id="divNotification" onclick="$(this).toggle();">
<h3>Some Notification!</h3>
Testing 1234...
</div>
</body>
</html>
I see three alerts, so I know that jQuery is working, but all three alerts simply show “NULL”. If I click on the resulting div, the onClick fires but I get an “Object Expected” error.
What is going on here?
Ok. I figured this out. It’s pretty counter intuitive.
Basically, I ended up loading all relevant javascript libraries into the source frame. I then must explicitly reference the source frame by its name in order to access these methods.
If any javascript – even javascript inside the popup window – wants to access the popup window’s DOM, you must fully qualify it (or provide jQuery will the correct root object) for it to work.
For instance, here is my new notification.html:
Now, in my source frame’s referenced javascript library:
So, basically, it appears that javascript that is run inside a popup created from window.createPopup executes in the context of the parent window, rather than the popup window itself!