/    Sign up×
Community /Pin to ProfileBookmark

Accessing variables from remote page

Hello, I’m trying to set a variable which a remote page can access. I can do this, but I need to set this variable inside “[B]window.onload = functio(){[/B]“. When I do this the remote page can’t access it. This is a breakdown of what I’m doing:

[code]//—————————————————————- index.html

<script type=”text/javascript”>

window.wow = ‘lmao’;

window.onload = function(){
window.lol = ‘omg’;
}

</script>

<iframe width=”200″ height=”200″ src=”frame.html”></iframe>

//—————————————————————- frame.html

<script type=”text/javascript”>

document.write(parent.wow); // outputs ‘lmao’
document.write(parent.lol); // outputs ‘undefined’

</script>[/code]

Any ideas why this might be happening?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@twseitexAug 12.2011 — Hi,


document.write() maybe create a new document


document.write(parent.wow); // outputs 'lmao'

object parent == object window ?

you mean window.parent (Object that receives the parent)

For a document, the parent is the containing window.

For a window defined using frame, the parent is the window that contains the corresponding frameSet definition.


window.wow = 'lmao'; // new porperty for window (or window.parent)



window.onload = function(){ ....} Fires immediately AFTER the client loads the object.

Internet Explorer 9 see http://msdn.microsoft.com/en-us/library/cc197055.aspx

In Internet Explorer 9, the type of event object depends on whether you use attachEvent or

addEventListener to register the event handler. If you register the handler with addEventListener,

an object of type Event is passed during the event. If you use attachEvent, the legacy event object

is passed instead.
Copy linkTweet thisAlerts:
@Logic_AliAug 13.2011 — Clearly the framed document is trying to read the variable in the parent before the parent has created it.

Use [I]setInterval[/I] to call a function that tries to read the variable in the parent, say every 500ms until it succeeds. When it does, cancel the interval and initiate whatever code uses the value.
×

Success!

Help @pea spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...