[Proto-Scripty] Re: Dynamic javascripts, IE7 problems...

2008-10-27 Thread kangax



On Oct 22, 5:19 pm, DaveC [EMAIL PROTECTED] wrote:
 Hi T.J,

 I'm not able to use the AJAX.Updater because of policy regarding AJAX
 requests require me to use another ajax. Suffice to say by the time I
 see the response it's two strings one with HTML and one with
 JavaScript.

 Here is the path I followed to get where I'm at. The following sample
 code (in my opinion) should correctly register the myfunction() and
 then allow me to call it. This is not the case and I get 'myfunction
 is not defined' in firefox, and the ever unhelpful Object Expected in
 IE.

 head
 script src=assets/js/prototype-1.6.0.3.js type=text/javascript/
 script
 script type=text/javascript

 Event.observe(window, 'load', function() {
         $('container').update(pNew HTML/p script alert('this will');
 function myfunction(){ alert('this wont!'); } \/script);

         // Fire the new function... does not work?
         myfunction();

 });

 /script
 /head
 body
   div id=container Old Content./div
 /body
 /html

 Swapping around, as the documentation suggested, to
 var myfunction = function(){ alert('this wont!'); }
 also fails. but here is where it gets interesting, both of the
 following work...

 eval(var myfunction2 = function(){ alert('this will 2!'); }; )
 myfunction2();

 eval(function myfunction3() { alert('this will 3!'); }  )
 myfunction3();

 So I'd have to conclude that prototype does not eval scripts in the
 same way as the eval() function does.?

 of course [hitting head on keyboard] that just gave me the solution...
 don't use prototype to inject the js. :)

 eval(new_content_js);
 var function_name =  'template_'+ template_name +'_onload';
 eval(function_name + '();' );

 Still an interesting exercise and life experience.


The problem is that `String.prototype.evalScripts` (which is called by
`Element.update`) executes native `eval` in its *own* scope. `eval`
happens to work in such way that it executes its code within scope of
a caller. In our case, that caller's scope is anonymous function
executed within `evalScripts`. Any function (or variable) declarations
(as in your example) are declared as local ones. In other words they
are lost once anonymous function exits its execution context : )

A workaround is actually pretty simple. If you need to declare a
global variable - define it as a property of global object:

someElement.update('script type=text/javascriptwindow.foo =
function(){ return \'foo\' }\/script');

// or via `this` from within an anonymous function

someElement.update('script type=text/javascript(function()
{ this.foo = function(){ return \'foo\'; }})();\/script');

 Thanks

 DaveC

[snip]

--
kangax
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Dynamic javascripts, IE7 problems...

2008-10-22 Thread T.J. Crowder

Hi Dave,

So in essence, you want to load a page (really a fragment of a page)
from the server into a container element and execute the scripts in
the page that comes back?  Good news:  You're working too hard. :-)
Prototype will do that for you if you set Ajax.Updater's evalScripts
option[1] to true.

[1] http://www.prototypejs.org/api/ajax/updater

You said you call a function on the page to give it a chance to run
its scripts.  You don't have to do that, any function calls at the
global level in the page that comes back will get executed as part
of Prototype's script evaluation.

Here's a demo:
http://pastie.org/297904  (the container page)
http://pastie.org/297905  (the page fragment it loads)

In that demo:  The container loads the newstuff fragment, Prototype
runs the scripts.  Since the newstuff fragment has a global call to
a function, that happens right then and we see the alert.  Once
loaded, the scripts on the fragment and in the main page have access
to each other, it's all the same DOM document.

HTH,
--
T.J. Crowder
tj / crowder software / com

On Oct 22, 9:42 am, DaveC [EMAIL PROTECTED] wrote:
 This is kind of related to previous post Dynamic script file
 loading, but I cant solve the following problem. Please help. It's
 basically an IE problem.

 I'm writing an AJAX application where each page of the site is loaded
 dynamically by the client side application. So for example the user
 clicks home, and the client side app makes an AJAX call to the server
 for the content and replaces the contents of a container div with this
 new content. Each page has some Javascript that needs to be
 dynamically added to the page as well, so here I'm adding a script
 element into the container as well. Once all that's done I call a
 predefined onload function in the returned script so that the page
 content can take over and do it's thing (setting up page related
 stuff)

 The difference with the previous post is that my javascript is
 returned in the AJAX call, I have no files to dynamically reference on
 the server, the Javascript comes in the AJAX response.

 BUT the following code hands in IE7 when it gets to the .update and I
 don't know why.. this otherwise works in other browsers.

             // add html to container.
             container.update(new_content_html);

             // add javascript to container.
             var el_script = new Element('script', { 'language':
 'JavaScript', 'type':'text/JavaScript' });
             el_script.update(new_content_js); // ?? IE7 hangs
 here ?
             container.insert( el_script );

             // call the template load event.
             var function_name =  'template_'+ template_name
 +'_onload';
             eval(function_name + '();' );

 As an aside I did, previous to this solution, try to combine the page
 HTML and inline Javascript and do a single update.
 container.update(new_content);
 however my _onload functions would not be registered in the browser
 causing the call the template load event part above to fail, dispite
 Prototype docs saying that Update evals inline scripts, well it does
 but the functions would not register.

 So either a solution to the first or an explination of the second
 method would be most appretiated.

 Thanks heaps every one.

 DaveC
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Dynamic javascripts, IE7 problems...

2008-10-22 Thread T.J. Crowder

Hi,

 
 // This kind of script WILL work if processed by Ajax.Updater:
[snip]
 That’s a common trickster, biting beginners in the ankle. So watch
 out!
 

Thanks for pointing that out.  As far as I can tell, that part of the
page (for lurkers: it's a quote from the Ajax.Updater page) is
incorrect or out of date as of the current version of Prototype,
1.6.0.3.  Both kinds of declarations work fine (on FF3, IE6, Safari
3.1.2, and Opera 9.1 -- all on Windows).  I'll file a documentation
ticket on Lighthouse about it; it needs to be removed or explained
more thoroughly.
--
T.J. Crowder
tj / crowder software / com

On Oct 22, 12:05 pm, DaveC [EMAIL PROTECTED] wrote:
 
 // This kind of script WILL work if processed by Ajax.Updater:
 coolFunc = function() {
   // Amazing stuff!

 }

 That’s a common trickster, biting beginners in the ankle. So watch
 out!
 

 (reading retard)  :) thanks.. I'll try this tomorrow and report back.

 DaveC

 On Oct 22, 11:00 pm, T.J. Crowder [EMAIL PROTECTED] wrote:

  Hi Dave,

  So in essence, you want to load a page (really a fragment of a page)
  from the server into a container element and execute the scripts in
  the page that comes back?  Good news:  You're working too hard. :-)
  Prototype will do that for you if you set Ajax.Updater's evalScripts
  option[1] to true.

  [1]http://www.prototypejs.org/api/ajax/updater

  You said you call a function on the page to give it a chance to run
  its scripts.  You don't have to do that, any function calls at the
  global level in the page that comes back will get executed as part
  of Prototype's script evaluation.

  Here's a demo:http://pastie.org/297904 (the container 
  page)http://pastie.org/297905 (the page fragment it loads)

  In that demo:  The container loads the newstuff fragment, Prototype
  runs the scripts.  Since the newstuff fragment has a global call to
  a function, that happens right then and we see the alert.  Once
  loaded, the scripts on the fragment and in the main page have access
  to each other, it's all the same DOM document.

  HTH,
  --
  T.J. Crowder
  tj / crowder software / com

  On Oct 22, 9:42 am, DaveC [EMAIL PROTECTED] wrote:

   This is kind of related to previous post Dynamic script file
   loading, but I cant solve the following problem. Please help. It's
   basically an IE problem.

   I'm writing an AJAX application where each page of the site is loaded
   dynamically by the client side application. So for example the user
   clicks home, and the client side app makes an AJAX call to the server
   for the content and replaces the contents of a container div with this
   new content. Each page has some Javascript that needs to be
   dynamically added to the page as well, so here I'm adding a script
   element into the container as well. Once all that's done I call a
   predefined onload function in the returned script so that the page
   content can take over and do it's thing (setting up page related
   stuff)

   The difference with the previous post is that my javascript is
   returned in the AJAX call, I have no files to dynamically reference on
   the server, the Javascript comes in the AJAX response.

   BUT the following code hands in IE7 when it gets to the .update and I
   don't know why.. this otherwise works in other browsers.

               // add html to container.
               container.update(new_content_html);

               // add javascript to container.
               var el_script = new Element('script', { 'language':
   'JavaScript', 'type':'text/JavaScript' });
               el_script.update(new_content_js); // ?? IE7 hangs
   here ?
               container.insert( el_script );

               // call the template load event.
               var function_name =  'template_'+ template_name
   +'_onload';
               eval(function_name + '();' );

   As an aside I did, previous to this solution, try to combine the page
   HTML and inline Javascript and do a single update.
   container.update(new_content);
   however my _onload functions would not be registered in the browser
   causing the call the template load event part above to fail, dispite
   Prototype docs saying that Update evals inline scripts, well it does
   but the functions would not register.

   So either a solution to the first or an explination of the second
   method would be most appretiated.

   Thanks heaps every one.

   DaveC


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Dynamic javascripts, IE7 problems...

2008-10-22 Thread DaveC

Hi T.J,

I'm not able to use the AJAX.Updater because of policy regarding AJAX
requests require me to use another ajax. Suffice to say by the time I
see the response it's two strings one with HTML and one with
JavaScript.

Here is the path I followed to get where I'm at. The following sample
code (in my opinion) should correctly register the myfunction() and
then allow me to call it. This is not the case and I get 'myfunction
is not defined' in firefox, and the ever unhelpful Object Expected in
IE.

head
script src=assets/js/prototype-1.6.0.3.js type=text/javascript/
script
script type=text/javascript

Event.observe(window, 'load', function() {
$('container').update(pNew HTML/p script alert('this will');
function myfunction(){ alert('this wont!'); } \/script);

// Fire the new function... does not work?
myfunction();

});
/script
/head
body
  div id=container Old Content./div
/body
/html


Swapping around, as the documentation suggested, to
var myfunction = function(){ alert('this wont!'); }
also fails. but here is where it gets interesting, both of the
following work...

eval(var myfunction2 = function(){ alert('this will 2!'); }; )
myfunction2();

eval(function myfunction3() { alert('this will 3!'); }  )
myfunction3();

So I'd have to conclude that prototype does not eval scripts in the
same way as the eval() function does.?

of course [hitting head on keyboard] that just gave me the solution...
don't use prototype to inject the js. :)

eval(new_content_js);
var function_name =  'template_'+ template_name +'_onload';
eval(function_name + '();' );


Still an interesting exercise and life experience.

Thanks

DaveC

On Oct 23, 1:47 am, T.J. Crowder [EMAIL PROTECTED] wrote:
 Hi,



  
  // This kind of script WILL work if processed by Ajax.Updater:
 [snip]
  That’s a common trickster, biting beginners in the ankle. So watch
  out!
  

 Thanks for pointing that out.  As far as I can tell, that part of the
 page (for lurkers: it's a quote from the Ajax.Updater page) is
 incorrect or out of date as of the current version of Prototype,
 1.6.0.3.  Both kinds of declarations work fine (on FF3, IE6, Safari
 3.1.2, and Opera 9.1 -- all on Windows).  I'll file a documentation
 ticket on Lighthouse about it; it needs to be removed or explained
 more thoroughly.
 --
 T.J. Crowder
 tj / crowder software / com

 On Oct 22, 12:05 pm, DaveC [EMAIL PROTECTED] wrote:

  
  // This kind of script WILL work if processed by Ajax.Updater:
  coolFunc = function() {
    // Amazing stuff!

  }

  That’s a common trickster, biting beginners in the ankle. So watch
  out!
  

  (reading retard)  :) thanks.. I'll try this tomorrow and report back.

  DaveC

  On Oct 22, 11:00 pm, T.J. Crowder [EMAIL PROTECTED] wrote:

   Hi Dave,

   So in essence, you want to load a page (really a fragment of a page)
   from the server into a container element and execute the scripts in
   the page that comes back?  Good news:  You're working too hard. :-)
   Prototype will do that for you if you set Ajax.Updater's evalScripts
   option[1] to true.

   [1]http://www.prototypejs.org/api/ajax/updater

   You said you call a function on the page to give it a chance to run
   its scripts.  You don't have to do that, any function calls at the
   global level in the page that comes back will get executed as part
   of Prototype's script evaluation.

   Here's a demo:http://pastie.org/297904 (the container 
   page)http://pastie.org/297905 (the page fragment it loads)

   In that demo:  The container loads the newstuff fragment, Prototype
   runs the scripts.  Since the newstuff fragment has a global call to
   a function, that happens right then and we see the alert.  Once
   loaded, the scripts on the fragment and in the main page have access
   to each other, it's all the same DOM document.

   HTH,
   --
   T.J. Crowder
   tj / crowder software / com

   On Oct 22, 9:42 am, DaveC [EMAIL PROTECTED] wrote:

This is kind of related to previous post Dynamic script file
loading, but I cant solve the following problem. Please help. It's
basically an IE problem.

I'm writing an AJAX application where each page of the site is loaded
dynamically by the client side application. So for example the user
clicks home, and the client side app makes an AJAX call to the server
for the content and replaces the contents of a container div with this
new content. Each page has some Javascript that needs to be
dynamically added to the page as well, so here I'm adding a script
element into the container as well. Once all that's done I call a
predefined onload function in the returned script so that the page
content can take over and do it's thing (setting up page related
stuff)

The difference with the previous post is that my javascript is
returned in the AJAX call, I have no files to dynamically reference on
the server, the Javascript comes in the AJAX response.

BUT the following