By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,893 Members | 778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,893 IT Pros & Developers. It's quick & easy.

Javascript into Ajax context... Unusual problem, no eval() issues

P: 1
Hi everyone!

First thank you for reading this post and yes, you probably already see that kind of topic title somewhere but the problem I've got today might be different than the 100 topics I've seen so far that did not resolve my problem...

Environment and problem: I have a page A with Ajax.js and a <div id="ajx">Content</div> that changes when clicking on a link. Another page, page B, gets loaded into page A and (tries) to load a javascript. When I click on the page A's link, the page B gets loaded but the javascript doesn't load.

Specification: I tried several things to get it work (load the javascript with the XHR request, load the javascript from an <img src="" onload="javascript.js">, eval();, global.eval(); with var global = this; to force the script to start, I also tried the script from various topics I've seen...) but nothing worked. Well it did when I tried to load the js with the XHR request but it did not load properly and since I must load the js at the end of page B and not at the beginning so it doesn't work.

Here are my scripts samples:

Expand|Select|Wrap|Line Numbers
  1. var loadedobjects=""
  2. var rootdomain="http://"+window.location.hostname
  4. function ajaxpage(url, containerid, tp, poststr){
  5. var page_request = false
  6. if (window.XMLHttpRequest) // if Mozilla, Safari etc
  7. page_request = new XMLHttpRequest()
  8. else if (window.ActiveXObject){ // if IE
  9. try {
  10. page_request = new ActiveXObject("Msxml2.XMLHTTP")
  11. catch (e){
  12. try{
  13. page_request = new ActiveXObject("Microsoft.XMLHTTP")
  14. }
  15. catch (e){}
  16. }
  17. }
  18. else
  19. return false
  20. document.getElementById(containerid).innerHTML='<img src="http://localhost/img/ld.gif">'
  21. page_request.onreadystatechange=function(){
  22. loadpage(page_request, containerid)
  24. }
  26. if (tp != 'POST'){
  27.'GET', url, true)
  28. page_request.send(null)
  29. } else {
  30.'POST', url, true);
  31. page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  32. page_request.setRequestHeader("Content-length", poststr.length);
  33. page_request.setRequestHeader("Connection", "close");
  34. page_request.send(poststr);
  35. }
  36. }
  38. function loadpage(page_request, containerid){
  39. if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
  40.     document.getElementById(containerid).innerHTML=page_request.responseText
  41. }
  43. function loadobjs(){
  44. if (!document.getElementById)
  45. return
  46. for (i=0; i<arguments.length; i++){
  47. var file=arguments[i]
  48. var fileref=""
  49. if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
  50. if (file.indexOf(".js")!=-1){ //If object is a js file
  51. fileref=document.createElement('script')
  52. fileref.setAttribute("type","text/javascript");
  53. fileref.setAttribute("src", file);
  54. }
  55. else if (file.indexOf(".css")!=-1){ //If object is a css file
  56. fileref=document.createElement("link")
  57. fileref.setAttribute("rel", "stylesheet");
  58. fileref.setAttribute("type", "text/css");
  59. fileref.setAttribute("href", file);
  60. }
  61. }
  62. if (fileref!=""){
  63. document.getElementsByTagName("head").item(0).appendChild(fileref)
  64. loadedobjects+=file+" " //Remember this object as being already added to page
  65. }
  66. }
  67. }
Page A
Expand|Select|Wrap|Line Numbers
  1. <a href="#" onclick="javascript:ajaxpage('up/load', 'ajx', 'GET'); loadobjs(alpha.js)">Upload</a>
  2. <br>
  3. <div id="ajx"></div>
Page B
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var ext_allowed='<TMPL_VAR ext_allowed>';
  3. var ext_not_allowed='<TMPL_VAR ext_not_allowed>';
  4. var max_files=<TMPL_VAR max_files>;
  5. var max_size=<TMPL_VAR max_size>;
  6. var descr_mode=<TMPL_VAR enable_file_descr>;
  7. </script>
  9. ##> Page design and information <###
  11. <iframe src="javascript:false;" name="alpha" style="position:absolute;left:-9999px;"></iframe>
  12. <script>InitSelector();</script>
So, for the page B, I need to load the first script with has no function but providing information and the InitSelector(); which is inside alpha.js. This is an atypic request but there must be something to solve it, init?

Thanks a lot in advance.. I've been chasing a solution for 20hours... dang! :-[

uNo! Weni
Nov 8 '07 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 15k+
P: 16,027
Split the JavaScript from the HTML. The HTML you can easily add. For the JavaScript, either use eval, or put in the head of the page.
Nov 9 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.