Search code examples
jqueryiframeparent

Reference parent from iframe to apply function to iframe


I looked around here and found this script to reference a parent from an iframe:

$(parent.document.body)

But how would I right a script that says, if the body of the parent has a certain class, make a link within my iframe go to a certain destination?

My iframe lives on the same server as the parent. I'm using this script once the parent body.class is defined:

$("#some-link").attr('href','entrant-list.html');

Just to be clear - my script lives in iframe, looks for class in parent doc, then sets a url on an element within iframe based on what class it found in parent doc.

Just need some help with the final code. Thank you.


Solution

  • in your iframe, have an onready script that does the following:

    $(function(){
        var pBody = $(parent.document).find("body")
        if(pBody.hasClass("foo")){
            $("#some-link").attr("href", "bar.com");
        } else if(pBody.hasClass("foo2")){
            $("#some-link").attr("href", "bar2.com");
        }
    }