views:

22

answers:

1

Hi all, I have an iframe with Js code to run, and i wan't to control the iframe DOM options and css style.
How can i do it? If you wan't to use jQuery, thats ok, but i preffer pure javascript...

Things that i want to modify:
- frameborder
- css styles (float, display, position, overflow)
- no scrolling bar

EDIT: The code run's inside the iframe

Thanks

+2  A: 

I assume from your previous questions that the document inside the iframe is not on the same domain as the outlying page.

In that case, your options are extremely limited, again because of the Same Origin Policy. You will not have Javascript access to the elements or CSS of the outlying page. That includes the <iframe> element on that page.

Any design decisions you will therefore have to put inline into the iframe tag:

<iframe frameborder="0" style="float: left; width: 500px; height: 200px ....">

I think that applies to everything you mention except the scroll bar behaviour: That you can control inside the iframe document using the body element's overflow property.

The only alternative idea that comes to mind is have the sites that embed your iframe also include a style sheet that comes from your domain in the head section of the document. That CSS you would, of course, fully control.

If you limit the design rules in that CSS to your iframe (e.g. by using the #id selector), that would work as well. The downside is that if the style sheet does not get included, your iframe will break. This is why most providers of widgets use inline CSS.

Pekka
so.. i will have to create a JS to create the iframe, receive the information on json, and apply it to the iframe?
CuSS
@CuSS yup, that would work I guess. Hadn't thought of that, good idea!
Pekka
But @CuSS in that case, you could dynamically generate the script already with the proper styles in it as inline. That will save you one request
Pekka
@Pekka my script loads the id of the ad, for example: `$('.MicrodualAdGet').attr('id');` will read from user DOM `<div class="MicrodualAdGet" id="3"></div>`, then i could create an iframe inside the div (or replace it), make the `GET` request with all information (size, on click url, image url, etc), and then replace iframe content with html code generated by javascript. I think this will bypass the SOP cookie problems...
CuSS
I will try it, thanks @Pekka, you're a great person, i must have your email ahaha
CuSS
@CuSS you can have my E-Mail, but only to buy me gifts from my Amazon Wish list! :P
Pekka
AHAHAHAHAHHA, @Pekka if i were rich, that would be ok AHAHHA
CuSS