Search code examples
javascriptsame-origin-policy

whateverorigin run html code


I'm trying to run a small application in an iframe using whatever origin:

    <script>
    $.getJSON('http://whateverorigin.org/get?url=' +      
    encodeURIComponent('http://vend.giftcardservices.nl/saldo/3') + 
    '&callback=?', function(data){
    alert(data.contents);
    });
    </script>   

And the alert shows the html is fetched, but when I try to document.write(data.contents), nothing is happening. How do I get this code to be executed in the browser?


Solution

  • You're using jQuery already, don't use document.write. Instead, use

    $(document.body).append(data.content);
    

    And if you're loading the content into a specific element, you could use

    $('#selector').html(data.content);
    

    Here's the snippet

    $.getJSON('http://whateverorigin.org/get?url=' +      
        encodeURIComponent('http://vend.giftcardservices.nl/saldo/3') + 
        '&callback=?', function(data){
        $(document.body).append(data.contents.replace('/static/js/saldo.js', 'http://vend.giftcardservices.nl/saldo/3/static/js/saldo.js'));
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>