简体   繁体   English

在页面加载时从 iframe 中移除焦点

[英]Remove focus from Iframe on page load

I have an Iframe embedded like this:我有一个像这样嵌入的 iframe:

<iframe id="foo" src="http://www.something.com" style="width: 90%; height: 300px"></iframe>

Each time the page loads focus gets lost from the top of the page and switches to this Iframe that is in the footer.I was wondering how can i remove the focus from this and make the page load "normally"?每次页面加载焦点都会从页面顶部丢失并切换到页脚中的这个 Iframe。我想知道如何从中删除焦点并使页面“正常”加载?

Thanks!谢谢!

Update: Yes, iframe is being loaded from another source (not the same domain)更新:是的,iframe 是从另一个来源(不是同一个域)加载的

if assumed the iframe is not served from the same domain.. you can place any other focusable dom element after the iframe in the footer with autofocus set as true .如果假设 iframe 不是来自同一域的服务..您可以在页脚中的 iframe 之后放置任何其他可聚焦的 dom 元素,并将autofocus设置为true And if that does not work please try the following in the parent main window:如果这不起作用,请在父主窗口中尝试以下操作:

$(window).on('load', function() {
    setTimeout(function(){$("body").focus()}, 100);
};

OR going by vanilla JS或者通过 vanilla JS

window.onload = function() {
    document.getElementById("some-focusable-element-from-parent-window").focus();
};

Well there is too much things we can do, for example on window load we scroll to the top with scrollTop function, ...etc But with such methods, where we take off the focus and move back to the top when the window is completely loaded, it will come with a glitch and no well effect.好吧,我们可以做的事情太多了,例如在窗口加载时,我们使用scrollTop函数滚动到顶部,...等但是使用这些方法,当窗口完全关闭时,我们会移开焦点并移回顶部加载,它会出现故障并且没有很好的效果。 After a while of thinking, and experiences, i come with that:经过一段时间的思考和经验,我得出以下结论:

We remove the iframe with display:none, when the dom is ready.当 dom 准备好时,我们使用 display:none 删除 iframe。 Next when the window is completely loaded, we bring it back.接下来,当窗口完全加载时,我们将其带回来。 To do that efficienly, we write a style class like that:为了有效地做到这一点,我们编写了一个这样的样式类:

    .display_none{
         display:none;
     }

and we will add this class to our iframe, when the document is ready, and remove this class when the window is fully loaded.当文档准备好时,我们将把这个类添加到我们的 iframe 中,并在窗口完全加载时删除这个类。 There is the code :有代码:

$(document).ready(function(){
    $("#myFrame").addClass("display_none");
    $(window).load(function(){
        $("#myFrame").removeClass("display_none");
    });
});

Now there is a situation where this will not work!现在有一种情况,这是行不通的! if the iframe is created and added with another script, then the dom may be loaded but not the iframe element itself (because the script may not have create it yet)!如果 iframe 是用另一个脚本创建并添加的,那么可能会加载 dom 但不会加载 iframe 元素本身(因为脚本可能还没有创建它)! And that was the case in my case .这就是我的情况。

SOLUTION: put the iframe in a container, let say a div container, and apply the method above to this container.解决方案:将 iframe 放入一个容器中,比如说一个div容器,并将上述方法应用于此容器。 All should work nickel!一切都应该工作镍! On dom load the div is hidden, next when the window is fully loaded, it restore it back!在 dom 加载时,div 是隐藏的,接下来当窗口完全加载时,它会恢复它! and you get your iframe, which will be loaded and get displayed.你会得到你的 iframe,它将被加载并显示出来。 No glitch, and efficient.没有故障,高效。

We can use sandbox attribute to block automatically triggered features (such as automatically playing a video or automatically focusing a form control).我们可以使用sandbox属性来屏蔽自动触发的功能(例如自动播放视频或自动聚焦表单控件)。

<iframe src="https://muthukumaran-m.github.io/" sandbox></iframe>

Check this for more info检查这个以获取更多信息

Assuming you have iframe in a same domain:假设您在同一个域中有 iframe:

You can do this :你可以这样做 :

$("#foo").on('load', function() {
    $("body",parent.document).focus()
};

It is already 2019 and I kept on having the same issue on some of my mobile websites.已经是 2019 年了,我在我的一些移动网站上一直遇到同样的问题。 The focus kept on being stolen by the contact form near the footer.焦点一直被页脚附近的联系表单窃取。

My rushed method may not be the most elegant way of doing this, but by modifying your iframe form to be hidden on mobile devices , with an expand/collapse button to make it visible on demand, you'll stop this jump from happening.我匆忙的方法可能不是最优雅的方法,但是通过修改您的 iframe 表单以隐藏在移动设备上,使用展开/折叠按钮使其按需可见,您将阻止这种跳转发生。

The simplified code:简化代码:

 <div class="d-block d-sm-none visible-xs"><!-- show only on mobile devices -->

   <a class="btn" data-toggle="collapse" href="#blockToControlID" role="button" 
    aria-expanded="false" aria-controls="blockToControlID">
    Expand/Collapse iframe Block
   </a>

   <div class="collapse" id="blockToControlID">
     <div class="card card-body">
       <iframe src="path-to-form"></iframe>
     </div>
   <div>

 </div>

 <div class="d-none d-sm-block hidden-xs"><!-- hide on mobile devices -->
   <iframe src="path-to-form"></iframe>
 </div>

For bootstrap 3 you would use visible-xs and hidden-xs to hide or show the snippets of code.对于引导程序 3,您将使用visible-xshidden-xs来隐藏或显示代码片段。

For bootstrap 4 you would use the "d-block d-sm-none" to show the mobile-only code;对于引导程序 4,您将使用“d-block d-sm-none”来显示仅限移动设备的代码; and "d-none d-sm-block" to hide the mobile-only code.“d-none d-sm-block”隐藏移动专用代码。

Scripts needed on footer:页脚所需的脚本:

 <script src="js/jquery/jquery.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
 <script src="js/bootstrap/bootstrap.bundle.min.js"></script>

The jQuery version will depend on your bootstrap 3 or 4 setup. jQuery 版本将取决于您的引导程序 3 或 4 设置。 popper.js must be loaded before bootstrap.js, otherwise it'll fail to work. popper.js必须bootstrap.js之前加载,否则将无法工作。

Prior to reaching this hack, I tried every possible thing.在达到这个 hack 之前,我尝试了所有可能的方法。 Sadly, none of the JS, jQuery and reCaptcha hacks posted by other members and forums worked.遗憾的是,其他成员和论坛发布的 JS、jQuery 和 reCaptcha hack 都没有奏效。 The page load would always default to the form.页面加载将始终默认为表单。

While the solution could be nicer with some js/ajax/jquery or even php, this simple code is quick and works fine with bootstrap enabled websites.虽然使用一些 js/ajax/jquery 甚至 php 的解决方案可能会更好,但这个简单的代码很快,并且适用于支持引导程序的网站。 Modify as needed.根据需要进行修改。

I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM