Javascript jquery ajax,如何在句柄页面调用onload 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6037217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:58:52  来源:igfitidea点击:

jquery ajax, how to call onload event in handle page?

javascriptjqueryonload

提问by cj333

I have try to post value from page a to page b via jquery ajax. But when I need a onload event in the handle page. I tried 2 ways, but all failed. How to call correctly or it is a bug?

我尝试通过 jquery ajax 将值从页面 a 发布到页面 b。但是当我在句柄页面中需要一个 onload 事件时。我尝试了两种方法,但都失败了。如何正确调用或者它是一个错误?

a.php

一个.php

<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">     
jQuery(document).ready(function(){
    $("a").click(function(){
     $.ajax({
         url: "b.php", 
         dataType: "html",
         type: 'POST', 
         data: "word="+"hello", 
         success: function(data){ 
            $("#show").html(data);
         }
      });
    });
});
</script>
<a href="#">click</a>
<div id="show"></div>

b.php

b.php

<script language="JavaScript">
   function callhello() {
       alert('<?php echo $_POST['word']; ?>');
       //many other code, for a test, I moved them. and replace a alert call.
   }
</script>
<body onload="JavaScript:callhello()"><!-- way 1, put onload here, not run -->
    Blah~~ Blah~~ Blah~~
<script language="JavaScript">// change to text/javascript or even remove, no effect
window.onload = function() {
  callhello();
};
</script><!-- way 2, put onload here, still not run. -->
</body>

回答by lonesomeday

When you insert HTML into your document, any scripts are executed with the document's context. That means that window.onloadrefers to the loadevent of the current window. Given that the insertion is delayed (by waiting for the clickevent), the window.onloadevent has already been triggered. You can attach a function to the loadevent, but it will never be triggered.

当您将 HTML 插入到文档中时,所有脚本都使用文档的上下文执行。也就是说,window.onloadload的是当前的事件window。鉴于插入被延迟(通过等待click事件),该window.onload事件已经被触发。您可以为load事件附加一个函数,但它永远不会被触发。

It's probably best to put the function call straight into your scriptelement:

最好将函数调用直接放入您的script元素中:

<script type="text/javascript"> // the language attribute is deprecated
  callhello();
</script>

This means that jQuery will execute the script as it's added to the document.

这意味着 jQuery 将在将脚本添加到文档时执行该脚本。

回答by Code Maverick

You need to change:

你需要改变:

<script language="JavaScript">

to:

到:

<script type="text/javascript">


You need to change:

你需要改变:

function callhello() {
    alert('<?php echo $_POST['word']; ?>');
    //many other code, for a test, I moved them. and replace a alert call.
}

to:

到:

function callhello() {
    alert('<?php echo $_POST["word"]; ?>');
    //many other code, for a test, I moved them. and replace a alert call.
}


You need to change:

你需要改变:

<body onload="JavaScript:callhello()">

to:

到:

<body onload="javascript://callhello();">