1

I'm trying to apply a CSS class that is defined in the head of an iframe to an element in the main document.

Is there an easy way - other than copying the class from the iframe's head to the main document's head with Javascript - to access that class and apply it to the main doc?

Something like this (which obviously doesn't work) in the main doc would be awesome:

<div class="#iframe.classtoapply">lorem ipsum</div>
Horen
  • 11,184
  • 11
  • 71
  • 113

1 Answers1

0

hey you can do like this.. Suppose you have a iframe which looks something like this having class a and b. CODE

<head>

<style>
.a{color:red;}
.b{
color:blue;
}
</style>
</head>

Now Suppose you have a Main page something like this containing that iframe.You can write code like this.Checkout.

 <head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
</head>
<body>
<iframe src="youriframe.html"></iframe>
<span class="a" >Hi</span>
<span class="b" >It Worked</span>
<script>
$(function() {
    $('head').append('<style/>');
    setInterval(function() {
        if(typeof($('iframe').contents().find('head>style').html())!='undefined') {
            $('head>style').eq(0).append($('iframe').contents().find('head>style').html());
        }
    }
    ,1);
}
);
</script>
</body>
Ashirvad
  • 2,367
  • 1
  • 16
  • 20
  • In the end I used a solution similar to @AshirvadSingh's approach. Seems like - even though it is not pretty - it is the best you can do. – Horen Oct 06 '12 at 15:59