2016-11-19 20 views
8

Tôi có một trang web (wordpress) có một vài nghìn bài đăng trên đó. Một bài đăng sẽ có dạng như sau:Vấn đề khung nội tuyến cần giảm thời gian tải trang

[tab:source1] 
<iframe width="560" height="315" src="https://www.youtube.com/embed/U3rKPexoEcA" frameborder="0" allowfullscreen></iframe> 
[tab:source2] 
<iframe src="https://player.vimeo.com/video/60718161?title=0&byline=0&portrait=0&badge=0" width="640" height="360" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> 
<p><a href="https://vimeo.com/60718161">HOW TO SHARPEN PENCILS</a> from <a href="https://vimeo.com/pricefilms">Pricefilms</a> on <a href="https://vimeo.com">Vimeo</a>.</p> 
[tab:source3] 
<iframe width="640" height="360" src="http://www.worldstarhiphop.com/embed/100588" frameborder="0" allowfullscreen></iframe> 

Thời gian tải trang đôi khi ở hai chữ số. Tôi hiểu tại sao điều này xảy ra nhưng tôi không chắc chắn làm thế nào để sửa chữa nó. Tôi muốn sửa chữa nó với một trong hai:

Iframe setTimeout() 

Hoặc: động asynch khung nội tuyến mà trông như thế này:

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

    // style the iframe with some CSS 
    iframe.style.cssText = "position:absolute;width:200px;height:100px;left:0px;"; 

    doc.open().write('<body onload="' + 
    'var d = document;d.getElementsByTagName(\'head\')[0].' + 
    'appendChild(d.createElement(\'script\')).src' + 
    '=\'\/path\/to\/file\'">'); 

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

    })(document); 
</script> 

Với một trong hai phương pháp làm thế nào tôi sẽ áp dụng điều này trên toàn cầu cho tất cả các iframe trên toàn bộ trang web? Tôi không nghĩ rằng có thể thay đổi từng iframe riêng lẻ.

Trả lời

0

Tôi đoán bạn nên thay thế tất cả các iframe bằng giải pháp tải mong muốn của bạn trong mẫu php trước khi xuất. Sử dụng regexp, như thế này <iframe.*src="([^"]*)".*\/iframe>. Tôi giả sử thay thế iframes chiều rộng div chứa tải gif và data-src thuộc tính với url iframe thực là giải pháp tốt. Sau đó, sử dụng js tải iframe bên trong div, sau khi tải trang.

Các vấn đề liên quan