0

I'm trying to verify if a XML document got modified after I loaded my page, like maybe every 30 seconds. I obtain the xml by making an AJAX request, and preferably, I want to compare the new one and the previous one before calling a function that displays the content into my HTML. How would I do?

 $.ajax(url, {
     method: "GET",
     type: 'xml',
     success: function(content){
         // Verify if the new xml content is the same as before, else 
         useContent(content, idContent)
     }
 });

What I tried:

success: function(content){
    if(previousXML === content) {
        return true;
    } else {
        previousXML = useContent(content, idContent);
    }
}

And this is how I return the content of the function useContent

 function useContent(xmlContent, id){ 
...
return xmlContent}

My current attempt which doesn't work:

if(previousXML === $thisBuildDate){
                    return true;
                } else {previousXML = useContent(content, idContent);

function useContent(xmlContent, id){
    let $lastBuildDate = $(xmlContent).find('lastBuildDate').text();
  return $lastBuildDate
}

I don't understand why it doesn't work since they're equal in my console, but else {previousXML = useContent(content, idContent); is still always executed.

Ron
  • 15
  • 4
  • What is the result of your try ? Did it work ? Try hashing the XML strings and checking the hash sum using the algorithm explained here: https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript – maksadbek Dec 01 '19 at 19:39
  • The result right now is that it detects that they are not the same xml file and it executes every time the other function. – Ron Dec 01 '19 at 19:50
  • Did you think of trying a regex? – pavan kumar Dec 01 '19 at 19:59
  • I tried taking the text of the buildDate of the xml document and compare it with the previous one, with no success. `let $thisBuildDate = $(content).find('lastBuildDate').text();`` if(previousXML === $thisBuildDate){ return true; } else {previousXML = useContent(content, idContent);` – Ron Dec 01 '19 at 20:25

1 Answers1

0

You could use the If-Modified-Since request-header.

$.ajax({
  method: 'GET',
  type:'xml',
  beforeSend: function(request) {
    request.setRequestHeader('If-Modified-Since', '18:45');
  },
...
Grim
  • 1,938
  • 10
  • 56
  • 123
  • How would I set the time to be when the page was loaded? And wouldn't it be `'If-Modified-Since'` – Ron Dec 01 '19 at 20:17