/    Sign up×
Community /Pin to ProfileBookmark

Executing a function in a parent frame

Okay, so here’s the deal. I have an iFrame on my page and I want it to make the parent frame execute a function as if the parent frame was executing it. The problem is I can’t simply go parent.doIt(), because all that does is execute the doIt() function in the current iFrame. Any suggestions? I tried parent.eval(“doIt()”), but that didn’t seem to work either. I’ve considered creating events and then artificially firing them on the parent frame, but this sounds complicated as it’s different for IE and for Firefox, so I want to avoid that if possible.

TIA.

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoAug 06.2006 — Either put the doIt() function into the parent frame page, or have the target be the parent frame:

<a href="#" target="parent" onclcik="doIt()">
Copy linkTweet thisAlerts:
@nanodeathauthorAug 06.2006 — Well, two things with that. First, the doIt() function is actually in an external .js file (which [I]is[/I] linked by the parent frame), and secondly, I don't want this in a link, I just want it to be able to execute normally between two <script> tags.
Copy linkTweet thisAlerts:
@konithomimoAug 07.2006 — The link was simply an example, but in your case, you can either place the script directly into the parent frame page, or you can do:

parent.doIt();
Copy linkTweet thisAlerts:
@nanodeathauthorAug 08.2006 — So...does anyone have any other ideas for this? Like I said, calling parent.doIt() doesn't call doIt in the parent frame, but rather executes the doIt() function in the current frame...not what I want :-/
Copy linkTweet thisAlerts:
@slaughtersAug 08.2006 — So...does anyone have any other ideas for this? Like I said, calling parent.doIt() doesn't call doIt in the parent frame, but rather executes the doIt() function in the current frame...not what I want :-/[/QUOTE]But he was not saying "just call parent.doIt()"

Embed it in the link where the target is the parent.

<a href="javascript:parent.doIt();" target="parent">

The doIt() function will be the one defined in the parent and any output from it will be directed to the parent.

If you are trying to do something weird like manipulate elements in the parent frame via your doIt function, then the only answere I can think of is to modify the doIt() code so that you refer to the parent elements in it explicitly , example: parent.document.getElementById(...
Copy linkTweet thisAlerts:
@nanodeathauthorAug 08.2006 — Alright...thanks...I can't have this stuff in a link though, since it has to just execute, not get clicked on. I'll figure something out.
Copy linkTweet thisAlerts:
@konithomimoAug 09.2006 — Don't you just love the people who watn the answer but won't give you the code to analyze???
Copy linkTweet thisAlerts:
@BrownBearAug 09.2006 — There are 2 ways I can think of:

1. IE specific:

parent.execScript("myfunction()","javascript")

  • 2. I tested it in IE and Firefox, it seems to work:

    parent.location.href="javascript:void(myfunction())"
  • Copy linkTweet thisAlerts:
    @nanodeathauthorAug 09.2006 — Alright, thanks muchly. That second idea hadn't occurred to me and seems like it would work (since it's basically like a force link, after all).
    Copy linkTweet thisAlerts:
    @BrownBearAug 09.2006 — actually, I have not tried it, but if you would have a hidden image or div, you van assign ="javascript:void(myfunction())" as src for image or background image url for div - don't see why it should not work
    Copy linkTweet thisAlerts:
    @slaughtersAug 09.2006 — There are 2 ways I can think of:

    1. IE specific:

    parent.execScript("myfunction()","javascript")

  • 2. I tested it in IE and Firefox, it seems to work:

    parent.location.href="javascript:void(myfunction())"
  • [/QUOTE]
    BUT - If myfunction() has a getElementById("mydivid") instruction in it, will it get the value of mydivid that resides in the parent, or the one which may, or may not, exist in the current frame?
    Copy linkTweet thisAlerts:
    @BrownBearAug 09.2006 — nanodeath ,

    can you please provide a code for case? I am trying to figure out why "parent." not working for you.

    I attached files with testing of different methods of access. take a look at it.

    [upl-file uuid=6287e05d-4a4e-4ed6-af1c-6fd4172bca43 size=1kB]Test.zip[/upl-file]
    ×

    Success!

    Help @nanodeath 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 5.4,
    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: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

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