Question

In parent.html I set an iframe with child.html in it.

in child frame,I write js:

$(function () { 
    var child = 6;
})

in parent frame,I write js:

$(function () {
    alert(window.frames[0].child);
});

but the alert result is "undefined"?

How can I quote another frame's variable correctly with jquery?

Was it helpful?

Solution

You're seeing undefined because you're delcaring it as a local variable (scoped only to that document.ready handler), you would need this in the child frame for a global:

$(function () { 
    window.child = 6;
});

Also, there's no guarantee that a document.ready on the parent frame won't execute before the child frame (stick an alert() in each to see the order)...in fact it should be the opposite. If you're not using it immediately, it's not an issue...if you are you need to execute it later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top