How to edit the CSS of an iframe?

Asked

Viewed 11,265 times

2

I need to put a iFrame on my website using the following code:

<iframe allowtransparency="true" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" src="http://www.cptec.inpe.br/widget/widget.php?p=3819&amp;w=h&amp;c=474647&amp;f=ffffff" height="180px" width="211px"></iframe>

How can I edit the CSS of the content that is displayed?

Wanted to change some colors, backgrounds, etc...

You could add a new CSS sheet?

Or add the CSS Incorporado same? Ex:

    <style rel="stylesheet" type="text/css">
    .classe { background: #000;
    }    
    #id { background: #FFF;
    }
    </style>

Iframe link: http://www.cptec.inpe.br/widget/widget.php?p=3819&w=h&c=474647&f=ffffff

  • 2

    iframe is in the same domain as your website?

  • 1

    I don’t know the security implications of CSS, but at first I’d say you can’t modify nothingness in the content of a iframe that does not belong to the same origin (schema, domain and maybe port) of your main page. The most you can do - if the browser supports - is to put the iframe in a sandbox, blocking Ajax, Javascript, etc.(in this case, for home page to protect against embedded malicious content). But arbitrary modifications, I’m afraid, impossible, and with good reason.

  • 1

    @mgibsonbr, I removed the answer... I thought it was possible

  • 1

    If you create a proxy type by loading the content using Curl, you could modify what you wanted before printing...

  • See if this can help you http://stackoverflow.com/questions/217776/how-to-apply-css-to-iframe

  • No @Sergio is on an external website.

Show 1 more comment

2 answers

3


I was able to do only with jquery using a proxy, now just work with css and script to work tabs, but this I leave with you:

jquery:

$('#divframe').load(
    'http://www.corsproxy.com/' +
    'www.cptec.inpe.br/widget/widget.php?p=3819&amp;w=h&amp;c=474647&amp;f=ffffff .tabtop',  function() {
         $('#divframe').html( $('#divframe').html().replace(new RegExp('src="', 'g'),'src="http://www.cptec.inpe.br/widget/') );
    });

HTML:

<div id="divframe"></div>

jsfiddle: http://jsfiddle.net/jaderw/kuLdmnm4/

EDIT: jsfiddle with tabs script working: http://jsfiddle.net/jaderw/kuLdmnm4/2/


Alternative with own proxy in PHP:

proxy.php (with just that content, and nothing else)

<?php
$cache_file = 'proxy_cache.html'; // nome do arquivo para salvar cache

if (!@file_exists($cache_file) || (time() - @filemtime($cache_file) > (60 * 60 * 6))) { // verificar se o cache expirou e fazer uma nova requisição ao cptec.inpe.br
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.cptec.inpe.br/widget/widget.php?p=3819&amp;w=h&amp;c=474647&amp;f=ffffff");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $retorno = str_replace('src="','src="http://www.cptec.inpe.br/widget/',curl_exec($ch)); // adicionar o endereço absoluto a todos os src
    curl_close($ch);
    file_put_contents($cache_file, $retorno); // salvar conteudo no arquivo de cache
} else { // cache não expirou abrir o conteudo do arquivo sem nova requisição ao cptec.inpe.br
    $retorno = file_get_contents($cache_file);
}

echo $retorno; // imprimir o conteudo
?> 

The content below should be in another file, together with css and everything else... ex file: html test.

jquery:

$('#divframe').load(
    'proxy.php .tabtop',  function() {
         $('#divframe .tab_content').hide();
         $('#divframe .tabs li:first').addClass('active');
         $('#divframe .tab_content:first').show();
         $('#divframe .tabs li').click(function(){
             $('#divframe .tabs li').removeClass('active');
             $(this).addClass('active');
             $('#divframe .tab_content').hide();
             $($(this).children('a').attr('href')).show();
         });
    });

HTML:

<div id="divframe"></div>
  • The Proxy you left is working 100%! I just don’t understand this part of the cache. Because this cache?

  • 1

    In order not to need to make a request to cptec.inpe.br for each pageview on your website... I put the update every 6 hours.

  • 1

    @Alexandrelopes Here for me the proxy.php is instant, just blinks, and I’m also running on the localhost...

  • 1

    @Alexandrelopes Make sure he’s creating the cache file, because if he picks up the cache, it should be faster still...

  • 1

    @Alexandrelopes has something wrong in your iframe.php because it is opening all the content that should only open in the proxy.php, then at the end there is an ajax request, calling by a proxy.php that does not exist on the server... I think you did not understand how it should be...

  • But the way I did (All in 1 only) got better, carries everything normal and no problems. Everything works 100%. Now what? Can I keep using like this? Or am I wrong?

  • 1

    Carefully read the answer, create the proxy.php file to be called by . load and use this filing cabinet as a basis for testing, just correct the jquery call <script src="js/jquery-1.11.1.min.js"></script> to point to a correct location/file.

  • 1

    This is right right there, now just fix the CSS as you wish, and to make the fade change the line $($(this).children('a').attr('href')).show(); for $($(this).children('a').attr('href')).fadeIn(); You don’t want me to have fun alone, right? Do a little too, after all the purpose of this is to learn how to do and not copy and paste.

  • Sorry, it’s just I don’t understand, there complicates it. kkkk... Your script is perfect, excellent, no comments.

  • It was perfect thank you very much! Now let’s delete some comments because we’ve made a lot of fuss, rsrs... You are very nice, thank you so much for spending your precious time with Noob here. kkk... vlw

  • look at this other: http://radioinhumasfm.com/iframe/frame2.html When you click on any tab the page gives a scroll alone.

  • It’s like I’m a link ancora pointing to tab_content

  • 1

    @Alexandrelopes try to change: $('#divframe .tabs li').click(function(){ for: $('#divframe .tabs li').click(function(event){ event.stopPropagation(); event.preventDefault();

  • It worked! You’re the guy, you’re awesome! Ebaaaa.... It’s that saved with cache then I thought I hadn’t paid, but it got 100%! You’re the guy!

  • I wish I was as wise as you... I’m N oob! :(

  • Heheh good that it worked @Alexandrelopes

Show 11 more comments

1

By definition, iframe allows you to open an external document, whether on the same server or on others. Some javascript solutions can help you do what you want, but only if it’s a document on the same server.

So in principle the answer is no.

But, you can create a copy of the external page on your server using PHP:

file_get_contents(/* string: url da página */)

And apply the CSS styles you want to the returned string, you can even create a "version" of the original page on your server, with your own styles and scripts.

  • Could you be more specific? I’m not very good with PHP, the ideal would be for you to publish more complete code, I don’t know what to put in /* string: url da página */

  • 1

    Does not work with relative JS and CSS like IFRAME

  • What do you mean @Papacharlie ? .

  • 1

    the widget needs JS to change tabs, CSS to images and the like... From what I saw in the widget, the paths are relative, you would need absolute paths

  • 1

    Take a test: file_get_contents( 'http://www.cptec.inpe.br/widget/widget.php?p=3819&w=h&c=474647&f=ffffff' )

  • I used: <?php&#xA;file_get_contents( 'http://www.cptec.inpe.br/widget/widget.php?p=3819&w=h&c=474647&f=ffffff' )&#xA;?> and nothing appeared, just gave white page.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.