Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Design

How to get a XMLHttpRequest into a JavaScript var???

Author Message
JohnGaver



Joined: 17 May 2005
Posts: 20
Location: Texas

Posted: Mon Feb 06, 2006 - 9:22 am    Post subject: How to get a XMLHttpRequest into a JavaScript var??? Reply with quote

I have a widget that I developed that uses a date that changes each year, in its calculations. So as to eliminate the requirement for users to download a new version each year, I am trying to come up with a way to read that date from a .txt file on the internet, whenever the widget is opened and store it in a JavaScript var.

I can use a XMLHttpRequest to get the date into the widget and display it on the face of the widget. But, I am having trouble figuring out how to store the retrieved date into a var that can be later used in the calculations.

This code works to display the date:
Code:
function gethttp() {
  var taxfreedom = new XMLHttpRequest();
  taxfreedom.open("GET", "http://www.actionamerica.org/taxecon/taxfreedomday.txt",true);
  taxfreedom.setRequestHeader("Cache-Control", "no-cache");
  taxfreedom.onreadystatechange=function () {
    if (taxfreedom.readyState==4) {
      var [b]tfdtxt[/b] = taxfreedom.responseText;
      document.getElementById("dat").innerHTML = tfdtxt;
    }
  }
  taxfreedom.send(null);
}

document.write("<h2>Date = <span id=\"dat\">?<\/span><\/h2>");

window.onload = gethttp;


The problem is that I don't want to display "tfdtxt". I need to read it into a var that I can parse for my date calculations. I have tried declaring it outside of "gethttp()" and several other things, but nothing seems to work.

Any ideas???
View user's profile Send private message Send e-mail Visit poster's website Widgets
Quill



Joined: 23 Feb 2007
Posts: 11

Posted: Thu Jul 17, 2008 - 3:45 am    Post subject: XMLHttpRequest & Storing Data Reply with quote

Do you just need to parse the response text before it is displayed, or do you need to store the data to access later?

For the former, once the request is complete, simply call a function that parses the data, passing in the response text as a parameter:

Code:

-------  In "gethttp()" -------

taxfreedom.onreadystatechange = function() {
   if (taxfreedom.readyState == 4) {
      if (taxfreedom.status == 200) {

         parseText(taxfreedom.responseText);

      }
      else {
         alert("Error: There was a problem loading the data.");
      }
   }
 }

-------  Later in the code -------

function parseText(text)
{
   // Code to parse the response text
}


Also, notice the extra "if" statement to check whether the request was successful or not (more info here).

On storing the data... If you just needed to store the data while the widget was running, you can just store the response text in a global variable. If the data has to persist after the widget is closed, or after the operating system is restarted, then you can store the data in a widget preference:

Code:

if (window.widget)
{
   // Save the data
   widget.setPreferenceForKey(taxfreedom.responseText, "MyPref");
}


...and when the widget starts up again, you can retrieve the data from the widget preference and place it into a global variable:

Code:

if (window.widget)
{
   // Retrieve the data and store it in a global variable named "MyPref"
   MyPref = widget.preferenceForKey("MyPref");
}


You can find out more about widget preferences at Apple's documentation here.


Does that help you?

Quill
View user's profile Send private message Send e-mail Visit poster's website Widgets
thewilson



Joined: 28 Aug 2008
Posts: 18

Posted: Thu Aug 28, 2008 - 3:58 pm    Post subject: Reply with quote

You are going a very long and complicated route to handle date. JavaScript has built in functionality for dates google found this site
http://bytes.com/forum/thread91573.html
and heres some code

Code:

<html>
<body>
<form>
<script type="text/javascript">
var now=new Date();
now.setDate(1);
var month=now.getMonth();
var year=now.getFullYear();
var firstDay=now.getDay();
var firstWed=1+(3-firstDay+7)%7;
var thirdWed=firstWed+14;
var data = "The first wed of this month is " + firstWed;
document.write(data);
</script>
</form>
</body>
</html>



Just modified from that post. To be easier to consume. Date goes from 0=Monday to 6=Sunday just change the 3 in the firstWed formula to the appropriate text date. This will be a much speedier solution.
View user's profile Send private message Send e-mail Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group