Current location: Hot Scripts Forums » General Web Coding » JavaScript » Add javascript after load?


Add javascript after load?

Reply
  #1 (permalink)  
Old 03-17-06, 04:09 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Add javascript after load?

Hi,

The idea with the page im working with (co. intra) is that it should never have to load again. All functionality is behind the scenes php and calling it javascript (XMLHttpRequest / Ajax).

I have a form that "submits" with javascript, values to JS vars and sent by ajax methods as POST to a php file.
The php file does some soap action and return eather error or success.

Now the problem.
Incase theres errors, i want to keep the form as it is. if not, i want to reload it.

why this is a problem is simply becourse i wouldint want to go edit the ajax function to check if i got errors back.
Currently the setup is like this:
Code:
+---------+------------+
| F O R M |return data|
+---------+------------+
If there are errors, they are outputted in the return data section along with javascript that reenables the submit button.
Incase there are no errors im outputting javascript that reloads the form, but it seems neather of thease are executed.

Does all javascript have to be loaded to the page at the initial load or is it possible to add javascript at a later time?

Last edited by <?Wille?>; 03-17-06 at 04:13 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-20-06, 03:26 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
It doesn't matter when a script is added, it is executed as soon as the browser parses the code for it.

Wouldn't it be easier to have the script which re-enables the button already on the page, and simply call it when an error is encountered?
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-23-06, 05:32 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Well, seems like it doesint.

soap is called with xmlhttprequest. Any response is outputted in a div with
document.getElementById('div').innerHTML = response;

response is eather
on success:
<script type="text/javascript">RefreshTehFormz('theform');</script>

on error:
The message
<script type="text/javascript">document.getElementById('submitbutton' ).disabled=false;</script>

neather of thease are executed tho. Should i use some other method of writing the response?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-23-06, 08:34 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
If you go to www.w3schools.com, they have a good section on AJAX.

Instead of using the script, they use span tags with ids to identify the content, then use the innerHTML property to assign the data.

I suspect the script tags aren't working because the page was already loaded. You could check by sending back an alert('test value'); to see what it does.

Instead of having the response data refresh the form, it might be better to call the refresh directly based on the response. This is also more modular.

Good luck.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-24-06, 03:40 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Everything added to the .innerHTML property is parsed by the browser, no matter when it was added. I use it frequently.
Try removing type="text/javascript", I've had problems with that for some reason.
It would be easier to tell what's wrong if you supplied a sample page which has the same problem.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-27-06, 02:55 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
the ok, removed the type="text/javascript" and added language="javascript" but didnt work anyway.

the layout:
Code:
<table class="maintbl" style="width:100%;border-width:0px;height:10px" cellspacing="0">
  <tr>
	 <td class="contentcell">
	   <table style="width:100%" class="contentcell">
		  <tr>
			<td style="width:40%" class="contentcell" id="vkform">
			  <? require('vkform.html'); ?>
			 </td>
			 <td class="contentcell"><div id="SMerror" style="overflow: auto;"></div></td>
		  </tr>
		</table>
	   </td>
	 </tr>
 </table>
the form itself is prittey long and basicly fields and tables, it does not have a submit button tho, its a regular button with a onclick attribute
Code:
<input type="button" value="Create" id="vsubmit" 
   onclick="javascript:var vs=document.getElementById('vsubmit');vs.disabled=true;vs.value='Creating...';SMsubmit();" />
the function
Code:
	function SMsubmit() {
	  var type, fn, ln, co, pn, em, alias, trial, sdomain;
	  type = document.getElementById('vtype').value;
	  fn = document.getElementById('vfn').value;
	  ln = document.getElementById('vln').value;
	  co = document.getElementById('vco').value;
	  pn = document.getElementById('vpn').value;
	  em = document.getElementById('vem').value;
	  alias = document.getElementById('valias').value;
	  trial = document.getElementById('vtrial').value;
	  sdomain = document.getElementById('vsdomain').value;
	  
	  var postdata = 'vtype='+escape(type)
					 +'&vfn='+escape(fn)
					 +'&vln='+escape(ln)
					 +'&vco='+escape(co)
					 +'&vpn='+escape(pn)
					 +'&vem='+escape(em)
					 +'&valias='+escape(alias)
					 +'&vtrial='+escape(trial)
					 +'&sdomain='+escape(sdomain);
	  RequestXML('ShopHandler.php', postdata, 'POST', 'SMerror');

	}
calling RequestXML takes arguments, relative path to file, post data (if any), method, and element to which output response. Calls with AJAX

ShopHandler prittey much takes the vars, creates a shop calling soap and returns

PHP Code:

if ($Store->GetError()) {
   echo 
'<b>Errors:</b><br /><pre>'.$Store->GetError().'</pre>
         <script language="javascript">vs=document.getElementById("vsubmit");vs.disabled=false;vs.value="Create";</script>'
;
   echo (isset(
$_SERVER['REMOTE_USER'])&&in_array($_SERVER['REMOTE_USER'],$admins)?
         
'<b>Debug:</b><br /><pre>'.$Store->GetDebug().'</pre><br /><br /><pre>'.@$Store->soap->debug_str.'</pre>' '');
} else {
   echo 
'<script language="javascript">RefreshSMForm();</script>';

the javascript is written to its destination correctly but doesint excecute.

Last edited by <?Wille?>; 03-27-06 at 03:01 AM. Reason: lotsa tabs in the code, reformat
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-27-06, 06:09 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
I forgot to mention that the language attribute is deprecated and shouldn't be used at all. Usually, just <script> is enough since most browsers figure out what to do with the code anyway. But to be valid, the type attribute should be used.

Have you tried to simply send back an alert("Hello World!") script, like wirehopper suggested?

I'm assuming that the return data is posted back to the page where the forms and functions are, some AJAX applications use an iFrame or hidden frame as a cache for the loaded data. It doesn't look like that's the case here though.

btw, the javascript: part isn't needed in event attributes like onclick. It's only neccessary for href atributes and such where JavaScript code isn't expected.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-27-06, 07:11 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
<script>alert("hello");</script> gives me nothing.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-27-06, 08:55 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Another idea would be to have the server return a javascript function, and after the response from the server is received and the code is posted at the client, use the response handler to call the returned function code.

The problem seems to be in executing the code, not the code itself. By forcing execution with a function call to it, you should be all set. A quick test would be to modify the
Code:
<script>alert("hello");</script>
to be
Code:
<script>function runner(){alert("hello");}</script>
and then calling function runner() from SMError(?) (the code that accepts the server response).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 03-28-06, 01:33 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by wirehopper
Another idea would be to have the server return a javascript function, and after the response from the server is received and the code is posted at the client, use the response handler to call the returned function code.

The problem seems to be in executing the code, not the code itself. By forcing execution with a function call to it, you should be all set. A quick test would be to modify the
Code:
<script>alert("hello");</script>
to be
Code:
<script>function runner(){alert("hello");}</script>
and then calling function runner() from SMError(?) (the code that accepts the server response).
so basicly, this problem will be fixed the way i initialy didnt want to.
a simple if statement in the response handler will do it.

Thanks to TwoD and wirehopper for taking the time to post here.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
add javascript to a php form aburrows182 PHP 3 08-18-05 12:38 PM
How to add an option for multiple quantities using javascript dlogic JavaScript 1 06-29-05 06:29 AM
Order of vbscript and javascript in ASP marlin ASP 0 06-03-04 04:01 PM
JavaScript to load Applet Letsplaycardspartner JavaScript 1 04-15-04 10:20 AM
Reaaly stuck about javascript over frames muratisik JavaScript 1 12-14-03 12:58 PM


All times are GMT -5. The time now is 04:51 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.