views:

229

answers:

3

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.

+1  A: 

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">
...
MiffTheFox
won't this keep submitting over and over forever?
JacobM
Referring to the element with id "myForm" as "myForm" is not a particularly good practice and if I'm not mistaken, only works in IE. You should use document.getElementById('myform') instead. But otherwise, I think this makes sense.
MatrixFrog
JacobM: No, the "onload" event is fired when the page loads.
MatrixFrog
Right, but it seems like he wants to display the results as if the page submits back to itself (so you could submit it again if you wanted to). If the page submits as soon as it loads, and reloads itself, it will then submit itself again, and so forth. As I say in my response, you'll need to handle that on the server side.
JacobM
+1  A: 

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".

JacobM
+1  A: 

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>
Tom
This is the correct approach.
the.jxc
Great thanks. I got it finally working :)
Masi