简体   繁体   English

如何在页面加载之前首先在 function 中运行 httprequest?

[英]How can I run a httprequest inside function first thing before page loads?

Ive tried on load atribute in body tag without success我试过在body标签中加载属性但没有成功

<body onload=\"updateGroup()\">

nor function calling inside echo: echo "<script> updateGroup(); </script>";也不是 function 在 echo: echo "<script> updateGroup(); </script>";

I'm open to critics!我对批评持开放态度!

function updateGroup(){
            
            var http = new XMLHttpRequest();
            var url = \"../requests/updateGroup.php\";
            http.open(\"POST\", url, true);

            //Send the proper header information along with the request
            http.setRequestHeader(\"Content-type\", \"application/x-www-form-urlencoded\");

            http.onreadystatechange = function() {//Call a function when the state changes.
              if(http.readyState == 4 && http.status == 200) {
                
              }
            }
            http.send();
          }

I believe what your looking for is an iife .我相信您正在寻找的是iife This should run even before document.ready这甚至应该在 document.ready 之前运行

(updateGroup() {
 ....
})();

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

相关问题 页面加载后,如何在Chrome中运行JavaScript代码段? - How can I run JavaScript snippets in Chrome when the page loads? Ui-router加载状态,那么我该如何运行一个函数呢? - Ui-router loads state, then how can I run a function? 我有一个命名函数,单击时效果很好。 页面加载时如何在特定元素上运行它? - I have a named function which works great on click. How can I run it on a specific element when the page loads? 当使用 jQuery 加载页面时,如何调用 function? - How can I call a function when page loads with jQuery? 如何在加载后使用greasemonkey调用页面上的函数 - How can I get greasemonkey to call a function on a page after it loads 每次页面加载时如何启动 function? - How can I start a function everytime the page loads? 如何运行XMLHttpRequest,在页面加载之前将结果放入会话 - How to Run an XMLHttpRequest, Put Result in Session Before Page Loads 我如何让此setTimeout调用在页面加载时首先发生,然后等待五秒钟? - How can I make this setTimeout call first thing on page load THEN wait five seconds? 我如何使用javascript访问HTML中的HttpRequest - How can I acess HttpRequest inside Html using javascript Mocha:我如何构造要在安装应用程序代码之前运行安装代码的内容? - Mocha: How Can I Structure Things to Run Setup Code *Before* App Code Loads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM