473,695 Members | 3,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Undefined global array inside body tag

6 New Member
Greetings,

My first time on this forum. I hope someone can help me out.
I am trying to get 11 values from several different identically structured webpages. I have the list of webpages saved as a global array. I am trying to access the sites one at a time and acquire the data using a for loop saving each value into an array that was declared as a global variable. I am trying to access the site from Body of the html and place each value into a row of a table and then empty the array with the values and start the for loop over accessing the next site.
I tested to see that the page is being read properly and that the value array has the correct data in it. As far as I can tell that all works fine. However when trying to write it to my table it comes out as a table full of the word "undefined. "

I hope this is all clear. I removed some of the specifics from the code for privacy reasons. Any advice is appreciated.

Thanks

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <title>HTML searching</title><head>
  3. <script type="text/javascript" language="javascript">
  4.  
  5.   var sites = new Array("http://site1.com",http://site2.com","http://site3.com","http://site4.com");
  6.   var value = new Array();
  7.   var http_request = false;
  8.  
  9.   function makeRequest(url, parameters) {
  10.  
  11.    try {
  12.     netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  13.    } catch (e) {
  14.     alert("Permission UniversalBrowserRead denied.");
  15.    }
  16.  
  17.     http_request = false;
  18.     http_request = new XMLHttpRequest();
  19.     if (http_request.overrideMimeType) {
  20.       http_request.overrideMimeType('text/xml');
  21.     }
  22.     if (!http_request) {
  23.       alert('Cannot create XMLHTTP instance');
  24.       return false;
  25.     }
  26.     http_request.onreadystatechange = alertContents;
  27.     http_request.open('GET', url + parameters, true);
  28.     http_request.send(null);
  29.   }
  30.  
  31.   function alertContents() {
  32.     if (http_request.readyState == 4) {
  33.       if (http_request.status == 200) {
  34.  
  35.         var the_page = http_request.responseText;
  36.     //... do some things to parse through the html
  37.     for(var i=0; i<11; i++)
  38.     {
  39.       //parse through data to save 11 values
  40.       value[value.length]=parsedString;
  41.     }
  42.       //alert("TESTING " + value);
  43.       } else {
  44.         alert('There was a problem with the request.');
  45.       }
  46.     }
  47.   }
  48.  
  49. </script>
  50. </head>
  51. <body>
  52.  
  53. <table border=1>
  54.  <tr>
  55. <script>
  56.  
  57.     for(var i=0; i<4; i++)
  58.     {
  59.         makeRequest(sites[i], '');
  60.         for(var j=0; j<11; j++)
  61.           {
  62.           document.write('<td>'+value[j]+'</td>');
  63.         }
  64.  
  65.        document.write('</tr><tr>');
  66.     }
  67.     value.length=0;
  68. </script>
  69. </body>
  70.  
  71. </html>
  72.  
  73.  
Jan 20 '08 #1
10 3154
gits
5,390 Recognized Expert Moderator Expert
hi ...

since the ajax-call is async you don't have the retrieved values in your array right after initializing the call ... so you should write it from your alertContent method, that is called when the response is ready to use. you should note that with your current code the alert comes after your document.write?

kind regards
Jan 20 '08 #2
Remus
6 New Member
Thank you for your response.

If I put the block of code I have inside the body tag into my alertContents function would I be calling makeRequest recursively? Would one makeRequest function call end before the next got put onto the stack(if that's the correct term)? This seems like it might be slow or memory inefficient if that is what would really happen.

I would also have to initiate this by calling the function the first time somewhere outside.

I hope I am understanding you correctly.
Jan 21 '08 #3
gits
5,390 Recognized Expert Moderator Expert
nope ... it doesnt't call recursivly then ... you only have to assure that you got the response before you want to use it! ... so in your case you fill the values array ... but now you would need a mechanism that invokes the document-update function when you have all your values ready or you could (and probably should) avoid document-write and update the innerHTML everytime a response returns ... there is nothing memory-inefficient with that ... it is just an async call and we have to deal with the async data-flow:

when you send a XMLHttpRequest you tell it that onreadystatecha nge is something to do and usually when the readystate is 4 and the request-status is 200 you have the moment where you have your response. you have to chain that moments with your code in a way that you can rely on the response ... you shouldn't use timeouts or whatever to wait for something ... since we always know when the response is back :) ... another way would be to make a sync call ... but that is ugly and you loose nearly all advantages of ajax ...

kind regards
Jan 21 '08 #4
Remus
6 New Member
thanks for the help so far.

I implemented an 'updating' function and using innerHTML as you suggested. My only problem now is that my table does not show up properly. The function is just a for loop that looks like this:
Expand|Select|Wrap|Line Numbers
  1. function updatePage() { 
  2.   for(var i=0; i<11;i++) {
  3.     document.body.innerHTML += "<td>"+value[i]+"</td>";
  4.   }
  5.   document.body.innerHTML += "</tr><tr>";
  6. }
Something obvious that is wrong here? The line of data from the array appears properly with no spaces or anything but appears to be unaffected by the col tags.
edit: I realized I am assuming that "innerHTML+ =" appends to the bottom of the body tag. Is that a false assumption? The data is appearing underneath my table headers
Jan 21 '08 #5
gits
5,390 Recognized Expert Moderator Expert
innerHTML sets the inner HTML code of the node(tag) you specify ... so you should add it to your table ...

kind regards

ps: alternativly you could use the createElement() and appendChild() dom-methods ...
Jan 21 '08 #6
Remus
6 New Member
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <title>HTML searching</title><head>
  3. <script type="text/javascript" language="javascript">
  4.  
  5.   var sites = new Array("http://site1.com",http://site2.com","http://site3.com","http://site4.com");
  6.   var value = new Array();
  7.   var http_request = false;
  8.  
  9.   function makeRequest(url, parameters) {
  10.  
  11.    try {
  12.     netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  13.    } catch (e) {
  14.     alert("Permission UniversalBrowserRead denied.");
  15.    }
  16.  
  17.     http_request = false;
  18.     http_request = new XMLHttpRequest();
  19.     if (http_request.overrideMimeType) {
  20.       http_request.overrideMimeType('text/xml');
  21.     }
  22.     if (!http_request) {
  23.       alert('Cannot create XMLHTTP instance');
  24.       return false;
  25.     }
  26.     http_request.onreadystatechange = alertContents;
  27.     http_request.open('GET', url + parameters, true);
  28.     http_request.send(null);
  29.   }
  30.  
  31.   function alertContents() {
  32.     if (http_request.readyState == 4) {
  33.       if (http_request.status == 200) {
  34.  
  35.         var the_page = http_request.responseText;
  36.     //... do some things to parse through the html
  37.     for(var i=0; i<11; i++)
  38.     {
  39.       //parse through data to save 11 values
  40.       value[value.length]=parsedString;
  41.     }
  42.       //alert("TESTING " + value);
  43.       updatePage();
  44.       } else {
  45.         alert('There was a problem with the request.');
  46.       }
  47.     }
  48.   }
  49.  
  50.  function updatePage() {
  51.   for(var j=0; j<11; j++)
  52.   {
  53.     document.body.innerHTML += '<td>' + value[j] + '</td>';
  54.   }
  55.   document.body.innerHTML += '</tr><tr>';
  56. }
  57. </script>
  58. </head>
  59. <body>
  60.  
  61. <table border=1>
  62.  <tr>
  63.  <th>Header 1</th>
  64.  <th>Header 2</th>
  65.   ....
  66.  <th>Header 11</th>
  67.  </tr>
  68.  <tr>
  69. <script>
  70. /*
  71.     for(var i=0; i<4; i++)
  72.     {
  73.         makeRequest(sites[i], '');
  74.     }
  75. */
  76. makeRequest(sites[0]);
  77. </script>
  78. </body>
  79.  
  80. </html>
  81.  
  82.  

Here is the code as it has changed. Just to keep things clear. I am assuming it would put the col tags right under the row tag but that is not what seems to be happening. I had everything on the page print out inside <xmp> so that I could see the tags.

The headers come out fine but after makeRequest is called the are changed to look like this:

&lt;table border=1&gt;&lt ;tr&gt;&lt;td&g t;

and then under that comes out my innerHTML stuff as

<td>value 1</td><td>value 2</td> .... <td>value 11</td></tr><tr>

and it looks fine. Do you know if that has anything to do with it? I will look into those alternatives you mentioned.

Many thanks
edit: My for loop is commented out and I am only calling it once to test it with only one of the site before I try reading it from all of them. Just to clarify.
Jan 21 '08 #7
Remus
6 New Member
I woke up today and decided to try to run the for loop that will read each of several web pages one after another (so I assume). I figured even if the table isn't working properly its no big deal. Just wanted to see it print out all the data.

It is only printing out the values from the last site. So if I run makeRequest passing it site[0] it gives me those 11 values if I run the for loop trying to get site1 thru site4 then it gives me just the 11 values for site4. I was thinking this might have something more to do with it being asynchronous again. Any thoughts?

Thanks
Jan 21 '08 #8
acoder
16,027 Recognized Expert Moderator MVP
The reason why you only get the results for the last request is that you're re-using http_request each time, so effectively you've only made one request - the last one.
Jan 23 '08 #9
Remus
6 New Member
Is there a way to do multiple page requests that you would suggest?
Jan 30 '08 #10

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

Similar topics

1
2226
by: Mark Anderson | last post by:
This is driving me crazy - I'm following through some basic rollover code (below with all extra content stuff stripped away. When I mouseover the image, IE errors, reporting "imagesHilite is undefined". Why? I've used the rem'ed out alert to prove the preLoad code runs and the mouse events pass the expected 'type' argument. Anyone show what I'm doing wrong? ######### code starts ######### <html><head><title>Image Swap Test</title>...
8
4582
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
3
3350
by: jimmygoogle | last post by:
I posted earlier with a scope problem. I think I resolved it in IE but in Firefox it still exists. Anyone have any ideas/experience with this? I attached my code sorry it is so long. You can cut/paste it into 2 files and run it to see what I mean. ###############menu.html############### <html> <body> <script type="text/javascript">
17
3342
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
45
4841
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9 of Books of ECMA :-)
5
11817
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
4
2766
by: nickyspace | last post by:
HI all I have a little issue with this php code. Below is the code CODE: PHP 1.<?
4
8407
hemantbasva
by: hemantbasva | last post by:
We have designed an aspx page having five ajax tab in it. the data of first four are designed on page whereas for the fifth tab it renders a user control named MYDOMAIN. in the tab container's even onactivetabindexchanged we have called a method loadtabpanel() which is defined in javascript in same page.the problem is it sometime give the message load tab panel undefined. this error does not come regularly. it comes in usercontrol rendering . i...
4
6329
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59 Notice: Undefined index: args in /home/mattehz/public_html/acssr/trunk/inc_error.php on line 92 Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_error.php on line 92
0
9001
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8860
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,...
0
8832
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 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...
0
7672
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4348
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3018
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
2
2281
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1984
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 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.