Hi,
I am verry new to prototype and ajax.
This on of my first tests.
It works great.. only the javascript that got loaded by ajax doesn''t
execute..
And when I click on the button that is loaded with ajax it get: test2
is not defined..
What am I doing wrong?
from http://prototypejs.org/learn/introduction-to-ajax
<cut>this module also deals in a smart way with JavaScript</cut>
So I must be doing something wrong?
TIA,
Jan
My code:
_______________________________________
test1.htm
_______________________________________
<html>
 <head><TITLE>AJAX TEST</TITLE></head>
 <body>
 <script src="/javascript/scriptaculous/prototype.js"
type="text/
javascript"></script>
 <script language="javascript">
 function ajax_onsuccess(transport){
 var response = transport.responseText || "no response text";
 document.getElementById(''newContent'').innerHTML = response;
 }
 function ajax_onfailure(){
 alert(''Something went wrong'');
 }
 function btnOnclick(object){
 new Ajax.Request(
 ''test2.htm'',
 {
 method:''get'',
 onSuccess: ajax_onsuccess,
 onFailure: ajax_onfailure
 }
 )
 }
 </script>
 <button onclick="btnOnclick(this)">ajaxTest</button>
 <div id="newContent">
 </div>
 </body>
</html>
_______________________________________
test2.htm
_______________________________________
<button id="button"
onclick="test2();">test</button>
 <script language="javascript">
 document.getElementById(''button'').innerHTML =
''TEST2'';
 function test2(){
 document.getElementById(''button'').innerHTML =
''TEST3'';
 }
 </script>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2007-Feb-22  18:24 UTC
Re: Javascript loaded with ajax doesn''t execute..
Hey Jan,
Nothing in Ajax.Request auto-evals anything [1].  Your using innerHTML 
won''t eval scripts magically, as well.
If you''re fetching HTML with <script> tags, you can use this for
dealing
with your response:
$(''newContent'').update(response);
This WILL extract the <script> elements, and eval them 10ms after having 
updated innerHTML (as a DOM update safeguard).
Better yet, use Ajax.Updater [2] for your need.  It would look something 
like:
new Ajax.Updater(''newContent'', ''test2.htm'',
{
   method: ''get'', evalScripts: true });
Still better: if you''re ONLY fetching JS, make it a .js file and 
Ajax.Request to it: since your server will return a JS-compatible MIME 
response type (e.g. text/javascript), A.R will auto-eval responseText.
[1] http://prototypejs.org/api/ajax/request
[2] http://prototypejs.org/api/ajax/updater
-- 
Christophe Porteneuve aka TDD
tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
instead of all that Ajax.Request stuff just do this:
function btnOnclick(object){
new Ajax.Updater(''newContent'', ''test2.htm'',
{method:''get'',
evalScripts:true} );
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
Thx I tried this the Ajax.Updater solution. It works partially, The function test2 defined in the loaded page does not work! When I click the loaded butten I get the error: test2 is not defined. I still must be doing something wrong. On 22 feb, 20:21, "wiggles" <buchanan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> instead of all that Ajax.Request stuff just do this: > > function btnOnclick(object){ > new Ajax.Updater(''newContent'', ''test2.htm'', {method:''get'', > evalScripts:true} ); > > }--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
This seems to be similar to the problem I was having -- your test2
function is defined in a different scope when the script is evaled.
see: http://www.sergiopereira.com/articles/prototype.js.html, the end
of the section labeled: "Using the Ajax.Updater class"
try
<script type="text/javascript" >
test2 = function(){
 document.getElementById(''button'').innerHTML =
''TEST3'';
}
</script>
On Feb 23, 4:52 am, "Mahes"
<jan.decle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Thx
>
> I tried this the Ajax.Updater solution.
> It works partially,
>
> The function test2 defined in the loaded page does not work!
> When I click the loaded butten I get the error:   test2 is not
> defined.
>
> I still must be doing something wrong.
>
> On 22 feb, 20:21, "wiggles"
<buchanan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > instead of all that Ajax.Request stuff just do this:
>
> > function btnOnclick(object){
> > new Ajax.Updater(''newContent'',
''test2.htm'', {method:''get'',
> > evalScripts:true} );
>
> > }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
This works Thank you verry much.. It is logical idd that the function was in a different scope. Using a global function variable to solve that is a very good workaround. thx On 23 feb, 22:13, "John Devine" <johnjdev...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This seems to be similar to the problem I was having -- your test2 > function is defined in a different scope when the script is evaled. > see:http://www.sergiopereira.com/articles/prototype.js.html, the end > of the section labeled: "Using the Ajax.Updater class" > > try > <script type="text/javascript" > > test2 = function(){ > document.getElementById(''button'').innerHTML = ''TEST3'';} > > </script> > > On Feb 23, 4:52 am, "Mahes" <jan.decle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thx > > > I tried this the Ajax.Updater solution. > > It works partially, > > > The function test2 defined in the loaded page does not work! > > When I click the loaded butten I get the error: test2 is not > > defined. > > > I still must be doing something wrong. > > > On 22 feb, 20:21, "wiggles" <buchanan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > instead of all that Ajax.Request stuff just do this: > > > > function btnOnclick(object){ > > > new Ajax.Updater(''newContent'', ''test2.htm'', {method:''get'', > > > evalScripts:true} ); > > > > }--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---