/    Sign up×
Community /Pin to ProfileBookmark

Access javascript func from iframe problem with safari

I have a simple javascript function loadNPlay(‘file’) working fine.

parent.loadNPlay(‘file’);
on click event it simply plays the file in a player in the parent window. with IE and Mozilla its working fine but with Safari, when called from iframe this function doesn’t work(it does work, ie loadNPlay(..) when called in the same window, in safari)

I have tried:
parent.loacation.loadNPlay(..) as well, it doesn’t work.

I am sure i am missing soemthing in accessing the parent widow javascript.
any suggestion?
Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyFeb 08.2009 — OK, you may already know a lot of this, but let me make a few points... Generally, when a user function is created, it becomes a pseudo-[B]method[/B] of the current [B]window[/B] object. That is the definition of a global, or top-level, function -- a [B]method[/B] of the current [B]window[/B] object. This is why you have things like [B]window.setTimeout()[/B] and [B]window.alert()[/B]. As a point of interest, the same applies to global user variables. They become psuedo-[B]properties[/B] of the current [B]window[/B] object.

Now, [B]window[/B] objects take on several forms and can be referenced by many pseudonyms -- depending upon which [B]window[/B] object you desire in a multi-window hierarchy. For example, [B]top[/B] is the pseudonym for the top-level window object which represents the full browser window. This window object is also the one reflected by the [B]url[/B] given in the browser's address bar. Some people used to take advantage of (abuse) this fact to "mask" the url address of the displayed document by employing a single-frame [B]frameset[/B]; because the browser's address bar would reflect the url of the [B]frameset[/B] and not the url of the document loaded in the [B]frame[/B] within that [B]frameset[/B]. And that gives us an example of another form of the [B]window[/B] object -- the [B]frame[/B] (also [B]iframe[/B]).

Two other pseudonyms, though (of the [B]window[/B] object), beside [B]top[/B], is [B]self[/B] and [B]parent[/B]. Thus, equivalent syntax would be both [B]window.setTimeout()[/B] and [B]self.setTimeout()[/B]. Then, [B]parent[/B], of course, would come into play when script, which is executing within a [B]frame[/B] or [B]iframe[/B], wishes to access objects, functions, or properties in the [B]window[/B] object which is the next level up from the current, or [B]self[/B], [B]window[/B] object. Now, if there are only two levels of [B]window[/B] objects, such as the stand-alone document with an [B]iframe[/B] in it, or a single [B]frameset[/B] document which contains only [B]frame[/B]s and no other [B]frameset[/B]s, then the use of [B]parent[/B] or [B]top[/B] becomes synonymous for referencing the [B]window[/B] object which is the next level up from the current [B]window[/B] object.

Now... All that is the norm. I give you that outline because the only possible fly in that works (since as you say it is working in IE and FF), is a thing known as default, or global, scope. There are two basic root objects -- [B]window[/B] and [B]document[/B]. The norm is to have the current [B]window[/B] object be the default scope. Thus, as I said, user functions and variables get attached to the current [B]window[/B] object as part of the default scope. But Netscape, by example and to the contrary, used to have a dual default scope depending upon whether you were executing from the HEAD section ([B]window[/B] scope) or from the BODY section of the document ([B]document[/B] scope). (Don't know what Netscape currently has.) So, the only recommendation I can give you is to find out if there is some kind of DOM inspector you can use on Safari to find out where your function resides -- in the [B]window[/B] object, in the [B]document[/B] object, or somewhere else.
Copy linkTweet thisAlerts:
@masipayauthorFeb 08.2009 — Thanks for elaboration, that was at expert level. I am using a simple function, and trying to call the function from iframe to parent window, was hoping there is a simpler way.

Inspecting the DOM object and function in parent window for simply calling a function seems like costly and beyond my intension right now ?, There must be a way, my friends tells me, "It works with IE, it works with FF, just do parent.myfunc(..).." I keep telling him, its not working in Safari!!! Shall I smack him? (He won't shut up! ?
Copy linkTweet thisAlerts:
@MrNobodyFeb 08.2009 — Inspecting the DOM object and function in parent window for simply calling a function seems like costly and beyond my intension right now ?[/QUOTE]
Well, if you just want to try something, did you catch what I was saying in this part of my explanation?
Now, if there are only two levels of [B]window[/B] objects, such as the stand-alone document with an [B]iframe[/B] in it, or a single [B]frameset[/B] document which contains only [B]frame[/B]s and no other [B]frameset[/B]s, then the use of [B]parent[/B] or [B]top[/B] becomes synonymous for referencing the [B]window[/B] object which is the next level up from the current [B]window[/B] object.[/QUOTE]
Copy linkTweet thisAlerts:
@masipayauthorFeb 08.2009 — Yes, i did notice that part. I am using only one iframe and turns out calling parent window as

parent.myFunc(..) from iframe is not the problem, Safari obeys this rule just like othe browsers. After debugging I found that other helper functions being called inside myfunc(..) was where safari was getting confused or was refusing to execute. This is related with java API of Jeroen's flash media player and i have tried to get their attention to this issue.

Thanks for your reply.
×

Success!

Help @masipay 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.17,
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,
)...