简体   繁体   中英

Javascript/HTML > Form/Input > automatic firing every time a page loads

I had to take my working example here. For some reason, it does not work as easily as the initial example.

New Example Suppose I want to see M5s every time the page loads. So how can I fire the same query for M5 every time the page load?

I copied the critical part here:

<body>
<div id="search">
         <form onSubmit="makeRequest(1); return false;" style="margin: 2px; padding: 2px; font-size: 1.2em;">   
        <input id="searchinput" type="text" name="tags" size="20" value="">
        <input id="searchbutton" type="button" onClick="makeRequest(1);" value="Create VideoWall"><br />
...
</form>
</div>

Response to the idea in MiffTheFox's and Tom's reply

So I added the command before the form above:

<body onload="document.getElementById('myform').submit();"> 

However, the wall stays black. It should be full of M5s.

Emerged problem to the initial Question: Why does it not work? Why does the wall stay black?

makeRequest asked by Tom

function makeRequest(page){
    startrequest = 0;
    for(i =1; i < 4; i++){
        clearList('ul'+i);
        var tags = encodeURI(document.getElementById('searchinput').value);

        if(i == 1 || i == 2){
            quantity = 45;
        }

        if(i == 3){
            quantity = 36;
        }
        insertVideos('ul'+i,'search',tags,quantity,startrequest);
        startrequest = startrequest + quantity;
    }
}

Please, see the url at the top and press CTRL+U to see the code.

First add an ID to the form, then add an onLoad handler that submits it.

<body onload="myForm.submit();">
<form id="myForm" name="input" action="form_action.asp" method="get">
...

Not sure what you're trying to accomplish, but you can certainly use jQuery to do

$(document).ready( function() { $("#submitButton").click(); });

The problem is ensuring that this only happens the first time the document is submitted; you will need to keep track of that on the server-side and remove the submission code after the first time.

A better approach is probably to compose your HTML on the server side so that whatever initial state you want to display is displayed. Many web applications have a form to submit a query of some kind (say, a search) but start with some initial sample result below the form. This is just created on the server side before loading, not by "pre-submitting".

Well, there´s on load attribute inside the body element

<body onload = "javascript:doSubmit()">
...
</body>

<script>
function doSubmit(){
var form = document.getElementById("myform");

if (form !=null)
  form.submit();

}
</script>

Also, you could add javascript at the end of your html page. This is not as portable as the first option

<html>
 <body>
  <form id="myForm" ...>
   ...
  </form>
 <script>
  //this executes when the page finishes loading
  var form = document.getElementById("myForm");

  if (form!=null) form.submit();
  </script>
 </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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