How to block pop-up, banner ads and video ads in iframe?

后端 未结 2 2106
伪装坚强ぢ
伪装坚强ぢ 2021-02-19 07:48

I\'m embedding video that has an exit pop-up banner ads and video ads. When you anywhere in video then popups open automatic or how to click on X icon to close

相关标签:
2条回答
  • 2021-02-19 07:54

    I used sandbox function in this code on my Streaming Site where i Embed 3rd Party iframe and they also have sandbox protection check but for that iv'e added removeAttribute in my JS so if you change src of the iframe from some other button you can click this button to add sandbox attribute to your iframe or you can also add the click function in your code where you get your iframe successfully.

    //JS
    window.onload = function(){
        var button = document.getElementsByName("sandbox")[0]
        var iframe = document.getElementsByName("framez")[0]
        button.addEventListener('click',sndbx,false);
    
        function sndbx(){
        var nibba = document.getElementById("framez").src;
        if(iframe.sandbox == 'allow-forms allow-pointer-lock allow-same-origin allow-scripts allow-top-navigation'){
        document.getElementById("framez").removeAttribute("sandbox"); 
        }
        frames['framez'].location.href=nibba;
        iframe.sandbox = 'allow-forms allow-pointer-lock allow-same-origin allow-scripts allow-top-navigation';
        }
    } 
    
    <!--HTML-->
    <button name="sandbox">SandBox</button>
    <iframe name="framez" id="framez" src="YOUR_SOURCE" allowfullscreen="true"></iframe>
    
    
    0 讨论(0)
  • 2021-02-19 08:00

    You can add sandbox attribute in your iframe. Only the values you add to the attribute will be allowed. Any value you do not add in the sandbox attribute will not be allowed by browser.

    Sandbox attribute has following values:

    allow-forms
    allow-pointer-lock
    allow-popups
    allow-same-origin
    allow-scripts
    allow-top-navigation
    

    I have modified your code to include sandbox option, but have NOT added allow-popups, so popups will not be allowed in this iframe.

    <div class="iframe">
       <iframe sandbox = "allow-forms allow-pointer-lock allow-same-origin allow-scripts allow-top-navigation" width="1000" height="600" src="https://www.youtube.com/embed/Sb_60g3u1LU" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>
    </div>

    You can find more about sandbox attribute here. Please note that this attribute is new in HTML5.

    0 讨论(0)
提交回复
热议问题