How to execute a page contain javascript function inside ajax response page?
Well, here I got stuck literally the whole day to sort out the problem that im facing now. Let me explain…
*photo.php - (Main php file)*
….
…. (header goes here…)
….
*<script src="scripts/photogallery.js" type="text/javascript"></script>*
….
…. (ajax refresh script goes here…)
….
**http1.open("GET", "photo_x.php?id1=" + escape(sIdtn1), true);**
….
…. ( body/coding goes here…)
….
…. **<script type="text/javascript"> initGalleryScript(); </script> // this script is inside the ajax refresh tag**
….
*Photo_x.php - (ajax refresh file)*
….
…. (coding goes here…)
….
…. **<script type="text/javascript"> initGalleryScript(); </script> // I need to execute this script which is not working currently.**
Kindly help me to find out a solution to execute a 开发者_运维知识库javascript function inside the ajax response page…
Thanks in advance.
Your question is not very clear what you really want to do, but judging purely by the subject of your question, I'll guess that you want to make an ajax request from your server and obtain some javascript back that you can execute in the context of your current page.
If all you need to get back from the server is data, then you can use JSON or JSONP which is a way of returning data from the server in javascript format where the data is expressed as javascript data structures that can just be executed and thusly parsed by the javacscript engine in the browser.
In some cases, the ajax request itself contains the name of a callback function in your main javascript that you want the JSONP response to call. Here a discussion of how this callback can work: http://s5.parature.com/UserGuide/Content/API/ParatureApiJSON.html#JSON_Callback_support.
If you write your own response handler for what is returned from the server, you can call eval on the returned code and you can put any sort of javascript you want in the response including code. There are some security things to watch for when doing that, but it can work this way too.
If the code is known ahead of time (which it usually is), then you probably don't actually need to return code from the server. You can either just include the code in the original page or load it dynamically by inserting script tags and then the ajax response can be just data.
精彩评论