473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Undefined global array inside body tag

6
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 3123
gits
5,390 Expert Mod 4TB
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
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 Expert Mod 4TB
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 onreadystatechange 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
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 Expert Mod 4TB
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
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&gt;

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
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 Expert Mod 8TB
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
Is there a way to do multiple page requests that you would suggest?
Jan 30 '08 #10
acoder
16,027 Expert Mod 8TB
Don't use the global variable. Use a local one, but then you'd probably have to make alertContents into an anonymous function so that the variable is accessible within it.
Jan 30 '08 #11

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

Similar topics

1
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...
8
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:...
3
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...
17
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
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...
5
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...
4
by: nickyspace | last post by:
HI all I have a little issue with this php code. Below is the code CODE: PHP 1.<?
4
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
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,...
0
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...

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.