说明:使iframe加载拒绝被嵌套的页面

在使用iframe时,某些站点无法被载入,只能显示出空白页面
如使用以下方式载入百度时:

<body>
    <iframe id = 'iframe' src = 'https://www.baidu.com'></iframe>
</body>

正确方式:

<body>
    <iframe id = 'iframe'></iframe>
    
    <script>
        var iframe = document.getElementById('iframe');
        
        iframe.onload = function () {
        
            if (this.getAttribute('is-reset') == '1') {  
                this.style.opacity = 0;
                this.style.display = 'block';
                var url = this.getAttribute('src-url');
                this.setAttribute('src', url);
                this.setAttribute('is-reset',0);
            } else {
                this.style.opacity = '1';
            }
        };
        
        window.onload = function(){
            changeUrl('https://www.baidu.com');
        }
        
        function changeUrl(url) {
            iframe.style.display = 'none';
            iframe.setAttribute('is-reset',1);
            iframe.setAttribute('src', 'about:blank');
            iframe.setAttribute('src-url', url);
        };
    </script>
</body>


- 美国低价服务器,G口带宽,稳定不丢包 -

Last modification:March 22, 2019
If you think my article is useful to you, please feel free to appreciate