SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selecting Iframe

    I have a document, with an iframe to another page. I cannot directly edit the content of the iframe, but I'd like to use Javascript on the parent page, to insert a stylesheet into the iframe page.

    I'm using the following code to add the stylesheet to the parent page, but I'm having no luck getting it to apply to the framed page.

    Code JavaScript:
    <script type="text/javascript">
    var addSS = document.createElement('link');
    addSS.rel = 'stylesheet';
    addSS.href = 'http://domain.com/style.css';
    addSS.type = 'text/css';
    document.getElementsByTagName("head")[0].appendChild(addSS);
    </script>

    I've tried making the last line:

    frames['iframename'].document.getElementsByTagName("head")[0].appendChild(addSS);

    And

    self.iframename.document.getElementsByTagName("head")[0].appendChild(addSS);


    Anyone have any suggestions?

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace your last line with this.

    Code:
    var iframe;
    if(document.frames)
    	iframe = document.frames["iframename"];
    else
    	iframe = window.frames["iframename"];
    iframe.document.getElementsByTagName("head")[0].appendChild(addSS);

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Justin! That worked a charm with Firefox!

    IE had trouble though. Think there's a way to help that?


    And thanks again!

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    took a while to figure out but IE likes to be a pain. This should work

    Code:
    var iframe;
    if(document.frames)
    	iframe = document.frames["iframename"];
    else
    	iframe = window.frames["iframename"];
    if(document.all)
    	iframe.document.createStyleSheet(addSS.href);
    else
    	iframe.document.getElementsByTagName("head")[0].appendChild(addSS);

  5. #5
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! That worked perfectly! Thanks a lot for taking the time to help me. I was at an utter dead end. xD


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •