473,746 Members | 2,678 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">Conten t</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="javascr ipt.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. 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. }
  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
1 3191
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

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 = {}; com.unFocus.Namespaces = new function() { this.register = function(namespace) { namespace = namespace.split('.');
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.