【问题标题】:How to remove onload event from the dynamically generated iframe?如何从动态生成的 iframe 中删除 onload 事件?
【发布时间】:2018-01-18 20:13:15
【问题描述】:
<div class="voc_container content-container">
<div class="voc_content">
    <h3 class="heading-small">Tell us what you think of this page</h3>
    <span class="voc_content_open">Take a short survey to give us your feedback</span>
    <span class="voc_content_close js-hidden">Close</span>
    <div class="voc_content_survey js-hidden">
    <script id="ss-embed-380173">
    (function (d, w) { 
    var s, ss;
    ss = d.createElement('script');
    ss.type = 'text/javascript'; 
    ss.async = true; 
    ss.src = ('https:' == d.location.protocol ? 'https://' : 'http://') + 
'www.xxxxxx.co.uk/s/r/embed.aspx?i=341674&c=380173';
};
    s = d.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ss, 
s); })(document, window);</script></div>
</div>
</div>**strong text**

生成html为

<iframe id="ss-embed-frame-380173" 
src="https://www.xxxxxxx.co.uk/s/YEXFX/" frameborder="0" style="border: 
0px currentColor; border-image: none; width: 100%; height: 400px; padding-
bottom: 4px;" onload="window.scrollTo(0, document.getElementById('ss-embed-
frame-380173').offsetTop);">&lt;a 
href="https://www.xxxxxxx.co.uk/s/YEXFX/"&gt;Please take our 
survey&lt;/a&gt;</iframe>

我尝试使用 createelement 删除列表器

  ss.removeEventListener("onload","window.scrollTo(0, 
  document.getElementById('ss-embed-frame-380173').offsetTop);");
 ss.onload=function(){} 

从动态生成的事件中删除 onload 事件,但不会删除。

谁能帮助我指导如何从动态生成的 iframe 元素中删除 onload 事件???

onload="window.scrollTo(0, document.getElementById('ss-embed-
frame-380173').offsetTop);

【问题讨论】:

    标签: javascript jquery asp.net iframe


    【解决方案1】:

    你可以使用 jQuery。

    <script type="text/javascript">
        $("#ss-embed-frame-380173").removeAttr("onload");
    </script>
    

    【讨论】:

      【解决方案2】:

      您可以使用MutationObserver 来观察添加到document 的元素,如果id 或其他选择器匹配,则在元素上调用.removeAttribute("onload").disconnect() 观察者实例

      <!DOCTYPE html>
      <html>
      <head>
      <script>
      const config = {
        childList: true,
        subtree: true
      };
      
      const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
          mutation.addedNodes.forEach(function(node) {
            if (new RegExp("^ss-embed-frame").test(node.id)) {
              node.removeAttribute("onload");
              observer.disconnect();
              console.log(node.getAttribute("onload"), node.outerHTML);
            }
          })
        })
      })
      
      observer.observe(document.documentElement, config);
      </script>
      <script>
      let iframe = `<iframe id="ss-embed-frame-380173" 
      src="data:text/plain,abc" frameborder="0" style="border: 
      0px currentColor; border-image: none; width: 100%; height: 400px; padding-
      bottom: 4px;" onload="window.scrollTo(0, document.getElementById('ss-embed-
      frame-380173').offsetTop);">&lt;a 
      href="https://www.xxxxxxx.co.uk/s/YEXFX/"&gt;Please take our 
      survey&lt;/a&gt;</iframe>`;
      
      onload = function() {
        document.body.innerHTML += iframe
      };
      </script>
      </head>
      <body>
      </body>
      </html>

      【讨论】:

        猜你喜欢
        • 2014-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        • 2011-10-09
        • 2020-06-05
        • 1970-01-01
        相关资源
        最近更新 更多