Current location: Hot Scripts Forums » General Web Coding » JavaScript » Loading Javascript from AJAX Responce


Loading Javascript from AJAX Responce

Reply
  #1 (permalink)  
Old 01-24-08, 09:24 AM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
Loading Javascript from AJAX Responce

I know when you using AJAX if you get some javascript from your reponce it doesnt run so you have to eval the javascript code out.

But how do i eval this responce???

Code:
<script language="javascript" type="text/javascript" src="Public/index.php?id=2"></script>
now the above code has inside some basic javascript of:

Code:
document.write('LOTS OF HTML HERE. NOTHING ELSE');
Any one have any ideas how i display the html of the above line in a DIV on my page after getting the responce from my AJAX??

If you need to say any other code let me know

Cheers
Reply With Quote
  #2 (permalink)  
Old 01-24-08, 09:48 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
There should only be the path to the file in the AJAX response, and then you create a script tag using DOM and add it to the DIV:

javascript Code:
  1. var path_to_js_file = /* response from AJAX */;
  2.  
  3. var script = document.createElement('script');
  4. script.addAttribute('type', 'text/javascript');
  5. script.addAttribute('src', path_to_js_file);
  6.  
  7. document.getElementById('your-div').appendChild(script);

(Untested)
Reply With Quote
  #3 (permalink)  
Old 01-24-08, 11:23 AM
scott2500uk's Avatar
scott2500uk scott2500uk is offline
Coding Addict
 
Join Date: Apr 2006
Posts: 275
Thanks: 2
Thanked 2 Times in 2 Posts
ok here is the code im using to test your method

Code:
function getPreview()
{

	var path_to_js_file = "Public/index.php?id=2";
	var script = document.createElement('script');
	script.setAttribute('type', 'text/javascript');
	script.setAttribute('src', path_to_js_file);
	document.getElementById('getPreview').appendChild(script);
}
now addAttribute didnt work so i presume you ment setAttribute??

the above code apears to do nothing in IE7 but no errors happen. In FF the code runs but the whole page is refreshed with the js file rather than putting it in my div that I have told it to go into.

Any Ideas?
Reply With Quote
  #4 (permalink)  
Old 01-24-08, 11:46 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Yes, my bad, I meant setAttribute().

However, is there a special reason why you're using Javascript to write the HTML? If not, then I'd just call the HTML file via AJAX request and put its content in the DIV. Without document.write().
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
For Hire (3 years exp.): Looking for Php, Mysql, AJAX, JavaScript, HTML, XML Barkat Job Offers & Assistance 1 09-25-07 04:15 PM
Need a programmer (Javascript, Ajax ...) wkristian Job Offers & Assistance 0 09-09-07 05:34 AM
How to recive mysql data using javascript ajax? method JavaScript 15 03-23-07 11:38 AM
AJAX javascript problemo maddude Script Requests 4 11-16-05 03:51 PM
blog about Javascript, AJAX, JSON jemptymethod JavaScript 0 07-21-05 09:13 AM


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