How to stop Page refreshing ?

Discussion in 'JavaScript' started by zed420, Feb 21, 2010.

  1. #1
    Hi all
    I have a very simple code to submit a form onLoad of a page, it works fine but IT keeps on refreshing every second, why ??? I really don't understand. :confused::confused:

    JS
    function myfunc () {
    	var form = document.getElementById("foo");
    	form.submit();
    	}
    Code (markup):
    HTML
    <body onload="myfunc()">
    <form id="foo"  action="EventData" method="post" ></form>
    Code (markup):
    Your help is much appreciated.
    Zed
     
    zed420, Feb 21, 2010 IP
  2. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #2
    I don't know javascript very well but I think this is what is happening ...

    ... the HTML load causes myfunc() to run

    ... myfunc() does a form.submit so the HTML starts up again, causing myfunc() to run again...

    I could be wrong.
     
    rayqsl, Feb 21, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It refreshes because the browser reads:

    //(page load so call command)
    1. onload() call function
    //(getElementById = get the forms name
    2. function get form name
    //(and submit form to server)
    3. submit form
    //(submiting a form cause the page to refresh so... go back to page load!)


    looks like you made an infinite loop. :)
    If you want it to stop refreshing try deleting the onload()
     
    Imozeb, Feb 21, 2010 IP
  4. Azzaboi

    Azzaboi Peon

    Messages:
    90
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You shouldn't be calling the myfunc() on the onload, but when the user has entered the details and pressed a button (onclick). The form.submit(); will refresh the page.
     
    Azzaboi, Feb 21, 2010 IP
  5. zed420

    zed420 Member

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Thanks for your replies,
    What I am trying to do is , I have a index.jsp page that has to show some data from database, because it's the first page customer views it has to be onLoad so in order to fetch this data I'm trying to trigger a hidden form. All works well apart from like I said before, keeps on refreshing and refreshing and refreshing ...

    Zed:mad:
     
    zed420, Feb 21, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hmm... from what I'm understanding you want to display data from a database without refreshing the page. You should use AJAX.
     
    Imozeb, Feb 22, 2010 IP
  7. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #7
    when the form is submitted and your data is loaded, Use your server-side code to add a hidden input box onto the end of the data, or whereever. e.g (html loaded from server-side code):
    
    ...database data....
    <input id="dataLoaded" type="hidden" />
    
    Code (markup):
    then change your code...
    
    function myfunc () {
    	var form = document.getElementById("foo");
    	if(!document.getElementById("dataLoaded")) form.submit();
    }
    
    Code (markup):
    The reason why it keeps refreshing, because when the form submits, the page is reloaded, therefore your code is executed again and the form is submitted again. Forming an endless loop.
     
    camjohnson95, Feb 23, 2010 IP