homepage Welcome to WebmasterWorld Guest from 54.197.183.230
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
asciispec in XMLHttpRequest
fm86

5+ Year Member



 
Msg#: 4296763 posted 9:49 am on Apr 13, 2011 (gmt 0)

Hi everybody!

I successfully executed an XMLHttpRequest and I need to kind of update it to check if the xml file (which is PHP generated) has changed. Which is the best way to do that?

I thought about getting the url information from the asciispec field and run again the codexhr.open("GET", url, true); but no idea about how to do that.
I tried to search but didn't find anything interesting.

Can someone help?

 

astupidname

5+ Year Member



 
Msg#: 4296763 posted 7:06 am on Apr 14, 2011 (gmt 0)

//get the xml files last modified date as a string:
var lastModified = xhr.getResponseHeader('Last-Modified');
//convert it to a date object and then to integer timestamp,
//number of milliseconds since jan 1, 1970 :
var lastModifiedTimeStamp = new Date(lastModified).getTime();
alert(lastModified +'\n'+ lastModifiedTimeStamp);


You would place the above into the xhr objects onreadystatechange function definition. What you do with the info from there is up to you I guess.

fm86

5+ Year Member



 
Msg#: 4296763 posted 11:33 am on Apr 14, 2011 (gmt 0)

Hi and thanks for the reply!

Still, this just gives me the timestamp. What I need is just the URL to which the xhr was connected.

astupidname

5+ Year Member



 
Msg#: 4296763 posted 12:02 pm on Apr 14, 2011 (gmt 0)

Sorry but you are being entirely too vague. How are we to know why you don't know to which url the xhr is connected? You make mention of an "asciispec field" but how are we to know exactly what that means? Read your mind for you? Url variable? or form field? "url to which the xhr was connected" meaning what? The xml file it's accessing? or the page from where it's running? "check if the xml file has changed" since when?

A bit more clarity from you and I'm sure you're almost there...

fm86

5+ Year Member



 
Msg#: 4296763 posted 12:20 pm on Apr 14, 2011 (gmt 0)

Well, sorry, English is not my mother tongue. Anyway, I created this xhr object and I would like to find the url of the xml to which the connection was sent so that I can run again the open function. This URL field seems to be contained in asciispec field but I can't access it.

In the worst case I just put the xml url in a global variable but maybe there is a more elegant solution. The question is: in the xhr object is there a field that contains the xml's url from which the object was built? And how to access this field?

astupidname

5+ Year Member



 
Msg#: 4296763 posted 1:09 pm on Apr 14, 2011 (gmt 0)

The question is: in the xhr object is there a field that contains the xml's url from which the object was built?


Good question, and the answer is that "no" the xhr object does not contain a property or method of retrieving it's url that it either is or was last connected to. So, yes, it is necessary for you to store the url being requested in a seperate variable to access later on.
As far as more elegant solutions, well the "elegantness" of them is debatable and leads to much more code than really needed. But you could create a simple object constructor which would store both the xhr and it's url:
function MyXhr(url) {
this.xhr = //code to get the xhr object
this.url = url;
}
//go on to describe prototype methods which would access the xhr methods...
//....
//....

var someXhr = new MyXhr('someXmlFile.xml'); //someXhr will have a 'url' property as well as an 'xhr' one

fm86

5+ Year Member



 
Msg#: 4296763 posted 1:49 pm on Apr 14, 2011 (gmt 0)

Thanks again, now everything is clear! And the elegant solution is very cute :)

Anyway, that's so weird, why wouldn't they automatically store an important information as to which xml file the object was connected? Guess there will be some interesting explanation to that. And why with firebug do I see this asciispec field containing the URL?

astupidname

5+ Year Member



 
Msg#: 4296763 posted 2:04 pm on Apr 14, 2011 (gmt 0)

hmmm... I don't see an "asciispec" field in an xhr object in firebug here. And yes, it does seem weird. Actually.... the xhr's responseXML property, when properly populated, does contain two properties: baseURI and documentURI which are the url of the xml file. Sadly they are not present in Internet Explorer of course and can't find alternative other than "build your own" solution as above suggested.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved