473,513 Members | 6,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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:

Ajax.js
Expand|Select|Wrap|Line Numbers
  1. var loadedobjects=""
  2. var rootdomain="http://"+window.location.hostname
  3.  
  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)
  23.  
  24. }
  25.  
  26. if (tp != 'POST'){
  27. page_request.open('GET', url, true)
  28. page_request.send(null)
  29. } else {
  30. page_request.open('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. }
  37.  
  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. }
  42.  
  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. }
  68.  
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>
  8.  
  9. ##> Page design and information <###
  10.  
  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
1 3173
acoder
16,027 Recognized Expert Moderator MVP
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

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

Similar topics

7
7421
by: Kevin Newman | last post by:
I've been toying with a namespace manager, and wanted to get some input. So what do you think? if (typeof com == 'undefined') var com = {}; if (!com.unFocus) com.unFocus = {}; ...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.