简体   繁体   English

在窗口焦点事件之前,在HTML的body标签中运行onload事件

[英]Run onload event in the body tag of HTML before the window focus event

I want to call an onload event before an focus event in a HTML page. 我想在HTML页面中的焦点事件之前调用onload事件。 In my following code 在我的以下代码中

<!DOCTYPE html>
<html>
<head>
<body onload='onloading()'>
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(window).focus(function(){

        callMe();
    });
});

function callMe()
{
    alert('Focus is on current page');
}

function onloading()
{
    alert('Onload function call ');
}
function myFunction() 
{
  document.write("some text");
}
document.getElementById("demo").innerHTML = myFunction(); 
</script>
</head>
<p id="demo"></p>
</body>
</html>

When I execute this code then i get an a pop up as "Focus on current page " and in the back ground " some text" . 当我执行此代码时,我会弹出一个对话框,显示为“关注当前页面”,而背景为“一些文本”。 But I expect to get an output in the following order 但是我希望按以下顺序获得输出

  1. "some text"" in the back ground 背景中的“一些文字”
  2. "Onload function call" popup “加载功能调用”弹出窗口
  3. "Focus is on current page" pop up . 弹出“焦点在当前页面上”。

But I am not getting the pop up "Onload function call" :( . I need to run the onload event in the body before the focus event. IS it possible to do such a thing ? 但是我没有弹出“ Onload函数调用” :(。我需要在焦点事件之前在主体中运行onload事件。这样做有可能吗?

Your HTML is invalid. 您的HTML无效。 You have put the head tag inside the body which is not allowed. 您已将head标签放置在body ,这是不允许的。

Make use of the following structure: 使用以下结构:

<html>
    <head>
        <script></script>
    </head>
    <body>
    </body>
</html>

Try this: 尝试这个:

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> </head> <body> <p id="demo"></p> <p id="focus">Click here</p> <script type="text/javascript"> function callMe() { $("#focus").html('Focus is on current page'); } function onloading() { alert('Onload function call '); } function myFunction() { $("#demo").html("some text"); } $(window).load(function () { onloading(); }); $(document).ready(function(){ $("#demo").html= myFunction(); $(window).focus(function(){ callMe(); }); }); </script> </body> </html> 

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

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