JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2061
Number of posts: 5164

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help: Get data from url and write to table Posted by littlemonsta on 24 Feb 2010 at 4:21 AM
Hi everyone,

First, I am not a programmer so be gentile. I need some help getting data from a url and placing it into a table. I am trying to get the daily threshold securities list that is published daily on the nasdaq site. The data published today is for yesterday's threshold securities. The url changes everyday according to the date eg: http://www.nasdaqtrader.com/dynamic/symdir/regsho/nasdaqth20100222.txt. The script that I managed to hack together does get the correct url everyday, but I am unable to insert the data into a dynamically created table. Here is what I have so far:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>ShoList</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

//create the Cross-browser XMLHttpRequest object
function getFile(pURL,pFunc) {
    if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
        xmlhttp=new XMLHttpRequest();
        eval('xmlhttp.onreadystatechange='+pFunc+';');
        xmlhttp.open("GET", pURL, true); // leave true for Gecko
        xmlhttp.send(null);
    } else if (window.ActiveXObject) { //IE 
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
        if (xmlhttp) {
            eval('xmlhttp.onreadystatechange='+pFunc+';');
            xmlhttp.open('GET', pURL, false);
            xmlhttp.send();
        }
    }

}

function makeTable() {
    if (xmlhttp.readyState==4) { 
        if (xmlhttp.status==200) { 
            var tmpArr=xmlhttp.responseText.split('\n');
            var out='<table id="theListTable" border="1" width="800" bordercolor="#DBDBDB" cellpadding="2" style="border-collapse:collapse;">';
            var tmp;
            var val;
            var txt;
			var strText;
            for (var idx=0;idx<tmpArr.length;idx++) {
                tmp=tmpArr[idx].split('|');
               /* out += '<tr><td>'+tmpArr[idx]+'</td></tr>';*/
				strText= '<tr>';
				for (var intTmp=0;intTmp<tmp.length;intTmp++)
				{
					if (intTmp<5){
					txt = tmp[intTmp].replace('"','');
					strText= strText + '<td>'+txt+'</td>';
					}
				}
				/*{
					if (intTmp<tmp.length -1){
						if (intTmp<5){
						txt = tmp[intTmp].replace('"','');
						strText= strText + '<td>'+txt+'</td>';
						}
					else 
						txt = tmp[intTmp].replace('"','')
						txt=''
						strText= strText + '<td>'+txt+'</td>';
						parent.shoDate=txt
					}
				}*/
				out += strText + '</tr>';
            }
            out += '</table>';
            document.getElementById('theList').innerHTML=out;
        }
    }

}

</script>
<script type="text/javascript">
function getFileUrl(){
/*First, get the current date*/
var currentTime = new Date();

/*because shoLists are published only at midnight during the workweek, we must subtract 1 from the day
In the case of Sunday, we subtract two, and Monday, subtracdt 3 in order to get Friday's list*/
var intNB = 1
if(currentTime.getDay() == 0)
{
	intNB=2;
}
else if(currentTime.getDay() == 1)
{
	intNB = 3;
}
else
{
	intNB = 1;
}	

/*We now need to subtract the number from the day to get the right date (Year, month, and day)*/
var day = currentTime.getDate() - intNB;
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();

/*If the day is less than ten, we must add a 0 to make the day a two digit number*/
if (day<10)
day="0"+ day

if (month<10)
month="0"+ month

/*Finally, we must put it all togther to create the proper URL to get the shoList file */
var shoDate=year + '' + month + '' + day

var strUrl='http://www.nasdaqtrader.com/dynamic/symdir/regsho/nasdaqth'
var strTxtUrl=strUrl+shoDate+'.txt'
/*
getFileURL=strTxtUrl


TEST*/
document.write(strTxtUrl)

}


</script>

<style type="text/css">
<!--
/* CSS Document */
.contenttext {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-weight: normal;
	text-transform: none;
	color: #333333;
}
.titletext {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
	text-transform: uppercase;
	color: #333333;
}

-->
</style>
</head>

<body onload="getFile(getFileUrl(),'makeTable');">
<div class="contenttext" id="theList">Loading...</div>
</body>

</html> 

Report
Re: Help: Get data from url and write to table Posted by mac_doggie on 1 Mar 2010 at 2:04 PM
I don't think ajax will work cross domain, so you'll need to call a script on your own website to get the data, or you might be able to use an iframe, but I think then you can't manipulate it's contents if it's on another domain as well...

To answer the question of reading data from the url and placing it in a table you could do something like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  <script type="text/javascript">
   function createTable() {
      var params = document.location.search.substr(1).split('&');
      var table  = document.createElement('table');
      var tbody  = document.createElement('tbody');
      table.appendChild(tbody);
      for(var i=0;i<params.length;i++) {
         var pair = params[i].split('=');
         var tr1 = document.createElement('tr');
         var td1 = document.createElement('td');
         var td2 = document.createElement('td');
         td1.innerHTML = pair[0];
         td2.innerHTML = pair[1];
         tr1.appendChild(td1);
         tr1.appendChild(td2);
         tbody.appendChild(tr1);
      }
      document.getElementById('container').appendChild(table)
   }



  </script>
  </head>
  <body>
  <div id="container"></div>
  <script type="text/javascript">
   // this script strips everything after ? and puts the fields in the first td and the value in the second td
   //
   createTable();
  </script>
  </body>
</html>


-mac-
mailto:mac_doggie@hotmail.com
the Netherlands...
Report
Re: Help: Get data from url and write to table Posted by littlemonsta on 2 Mar 2010 at 10:20 AM
Hi mac, Thanks for trying but I still can't get the info from the URL to a table. As I said before I am not a programmer and there are a lot of things that I just don't understand, I'm trying to learn. The script that I posted gives me the correct URL daily, but it writes the URL to the page instead of taking the contents of that URL and creating a table with them.
Report
Re: Help: Get data from url and write to table Posted by mac_doggie on 5 Mar 2010 at 11:17 AM
if you have the URL in your page, is it inside a div tag like:
<div id="contentsofothersite">
http://www.somesite.com?variable1=value1&variable2=value2
</div>

??

If this is the case you can use javascript to grab that line of text and split it up into pieces. You can use my example, but replace the document.location.search with documentGetELementById('contentsofothersite').innerHTML

where contentsofothersite is the ID of the div element that the URL is in.

hope this helps you out...

-mac-
mailto:mac_doggie@hotmail.com
the Netherlands...



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.