PDA

View Full Version : Execution of Javascript returned from AJAX call



varunach
18 Mar 2011, 3:10 PM
Hi,
In the application i'm building, I have a menu on the left and a view area in the remaining part of the screen. On click of an item, I want to load and execute a JS script,which contains Ext JS components, returned from the server .

I've tried the document.createElement method and the el.update({url : 'someurl.js', scripts: true}) method but these haven't worked for me.

The scripts returned by the server are pure JS scripts without any html tags (not even <script></script>)

Any ideas how this could be done?

Thanks.

varunach
18 Mar 2011, 3:21 PM
when i use


Ext.get('baseLayoutBody').load({
url : 'xyz.js',
scripts: true,
text : 'Loading.. '
});
The javascript text is returned as it is. It isn't processed.

varunach
18 Mar 2011, 3:47 PM
Ok the JS is getting loaded (browser alert is working), but the problem is that Ext JS components defined in the JS aren't getting loaded. Why is that?

walldorff
18 Mar 2011, 7:25 PM
Be sure that Ext is ready.
This post (http://www.sencha.com/forum/showthread.php?32369) might also be interesting.

varunach
18 Mar 2011, 9:13 PM
I already have the components written inside Ext.onReady().

Also, i had to add the <script> tags in the *.js file to make it work even though the Mime type is showing as text/javascript in firebug.

Thanks for your help.

varunach
18 Mar 2011, 10:05 PM
I got the problem, I had a little brat called renderTo in the code.. :D .. got it working now..

But i still have to put the <script> tags around the code. What do i do about that?

varunach
18 Mar 2011, 10:27 PM
Forget it.. i put the JS inside a JSP.. That solved the little sucker's problem. This way it's even better.. now i have the full power of a JSP too any my disposal..

Thanks..


For those who're trying to get it done too.. here's how to do it..



Ext.get('baseLayoutBody').load({
url : 'signUp.action',
scripts: true,
text : 'Loading.. '
});

baseLayoutBody is the <div> tag where the returned JSP is to be displayed.
signUp.action is the JSP i'm trying to load. I'm using an struts action to call the JSP.

If you only need to load a JS which in turn contain ExtJS components, then make sure to check the renderTo configs and the id's onto which you're trying to render the components. They shouldexist in the DOM before you are trying to load them. Running the script inside the Ext.onReady() takes care of that. Also, make sure that the DOM element onto which you're trying to load the components can support the structural requirements of each of the components.

Don't use the eval(response.responseText) because I heard that there are some security issues related to that.

Hope this helps!