动态异步iframe出现问题

时间:2013-09-10 00:36:52

标签: javascript jquery html iframe

我正在尝试使用Dynamic Async iFrame方法,但我的代码无效,我对我做错了什么感到困惑。 iFrame本身正在加载,但它没有显示预期的网页。请参阅what I mean

这是我的代码。我做错了什么?

<script>
    (function(d){
        var iframe = d.body.appendChild(d.createElement('iframe')),
            doc = iframe.contentWindow.document;

        // style the iframe with some CSS
        iframe.style.cssText = "width:900px;height:600px;";

        doc.open().write('<body onload="' + 
            'var d = document;d.getElementsByTagName(\'head\')[0].' + 
            'appendChild(d.createElement(\'script\')).src' + 
            '=\'http://www.uab.edu/dining/locations\'">');

        doc.close(); //iframe onload event happens

    })(document);
</script>

编辑:解决方案代码

<%= javascript_tag do %>
        $(window).load( function(){
            window.map = '<%= j @college.map.html_safe %>';
            window.street_view = '<%= j @college.street_view.html_safe %>';
            window.food = '<%= j @college.food.html_safe %>';
            window.housing = '<%= j @college.housing.html_safe %>';
            window.weather = '<%= j @college.weather.html_safe %>';
            $('#map').html(map);
            $('#street_view').html(street_view);
            $('#food_iframe').html(food);
            $('#housing_iframe').html(housing);
            $('#weather_section').html(weather);
        });
    <% end %>

1 个答案:

答案 0 :(得分:1)

尝试此jquery在Dom准备就绪时加载iframe

$(document).ready( function(){
   $('#placeholder').html('<iframe src=""></iframe>');
})

假设您有一个ID =占位符的占位符..

或全部加载(包括图像)

$(window).load( function(){
   $('#placeholder').html('<iframe src=""></iframe>');
})