Forum Moderators: open

Message Too Old, No Replies

Javascript action when page content is loaded.

Refresh a div element when another div element outputs a succes message

         

tomda

7:22 am on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I am trying to implement an ordering system using AJAX

Top DIV element - List of quotations/orders and status (quotation request in process, awaiting quotation from company, order to be confirmed by user, order confirmed by user, etc...)

Bottom DIV element - Show request/order content (this is where the user can add/modify products or fill forms to add delivery address and date or click button for action).

Example
A/ User click on "send quotation" on the top DIV
B/ Bottom DIV show a form with delivery date and address that user must fill before clicking on "Send"
C/ Bottom DIV show a success message "Your quotation request has been sent. Click here to refresh the top div"
D/ Top div is refreshed and status of the requests/orders modified

You got it... I want to get rid of the "click here to refresh top DIV". I have tried to use an onload event on a DIV element, I have also tried to write javascript in the body <script></script> but both methods failed.

To sum up, once all HTML is output, I need to run a function to refresh another div element automatically and get rid of the <a href="#" onclick="javascript:dosomething(); return false;">Click here to refresh top div</a>

Any suggestions or tips will be appreciated.
Thanks

Drag_Racer

7:27 am on Jul 10, 2007 (gmt 0)

10+ Year Member



in step c when you write the success message, add a setTimeout('doSomething()',3000) or something like that to the script

tomda

7:59 am on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your tip Drag_Racer...

In fact, I was just about to try this method.
Nonetheless, the problem is that any javascript should be written within the <body></body>

I had tried the following yesterday:
echo "<script>alert('hi')</script>";
and it was not working, therefore I guess that:
echo "<script>setTimeout('dosomething()',3000);</script>";
will also fail.

I will do some more testing today.

phranque

8:51 am on Jul 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



it is quite ok and normal to "write" or include javascript in the <head> of the document and if you are planning to do a <body onload="loadfunction()"> then you in fact must define loadfunction() before the body tag occurs.`

tomda

9:13 am on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK!
I have done more tests and it seems that there is no other alternative than having a link with event handler ("click it here to refresh top DIV element")

In the example example, no alert message unless I click on the link (it is an HTML output in my bottom DIV element using innerHTML)

OK! Your quotation request has been sent to us
<script type="text/javascript">alert('dede');</script>
<a href="#" onclick="javascript:alert('dede');">Click here to refresh top DIV element</a>

Thanks

Drag_Racer

3:04 pm on Jul 10, 2007 (gmt 0)

10+ Year Member



I am guessing you don't have access to the entire page?

javascript written to the page via ajax does not execute. there is a hack to get around this but only works in firefox and opera, so not an option

have you tried an iframe? a page called by the iframe src will be able to execute scripts. the limitation here is both pages must be from the same domain for one script to have access to the others function

tomda

6:11 am on Jul 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Drag_racer

Yep, you are right...

Javascript written to the page via ajax does not execute so I found a work-around, that is a function to basically load a .js object when calling the ajax page, function which inserts the javascript <script> in the main page using DOM.

Thanks for your help.
Cheers.