Skip to content

Instantly share code, notes, and snippets.

@jocastaneda
Created July 23, 2018 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jocastaneda/1764f81fd314ea440c10b505853ab576 to your computer and use it in GitHub Desktop.
Save jocastaneda/1764f81fd314ea440c10b505853ab576 to your computer and use it in GitHub Desktop.
<div class="msgHere">
... loading today's quote
</div>
<script type='text/javascript'>
var xmlhttp = null;
function AjaxRequest(url) {
if(xmlhttp != null){
if(xmlhttp.abort)
xmlhttp.abort();
xmlhttp = null;
};
if(window.XMLHttpRequest) // good browsers
xmlhttp=new XMLHttpRequest();
else if(window.ActiveXObject) // IE
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if(xmlhttp == null)
return null;
xmlhttp.open("GET",url,false);
xmlhttp.send(null);
if(xmlhttp.status >= 200 && xmlhttp.status < 300)// 2xx is good enough
return xmlhttp.responseText;
else
return null;
}
function clearEle(element){
while(element.hasChildNodes())
element.removeChild(element.firstChild);
}
window.onload = function(){
var el, msg, date1, date2, txt = AjaxRequest('/files/quotes.txt');
date2 = new Date();
date2.setFullYear(2011);// year to begin
date2.setMonth(5);// take 1 from the month number
date2.setDate(6); // date of month
if(txt == null)
msg = 'page not found or Ajax not supported by your browser.';
else{
msg = txt;
}
date1 = new Date();
txt = msg.split("\n");
date1 = Math.floor(date1.getTime() / (24*60*60*1000));
date2 = Math.floor(date2.getTime() / (24*60*60*1000));
if(date1 > date2){// swap them around? -- replace with another method?
var tmp = date1;
date1 = date2;
date2 = tmp;
};
el = document.getElementsByClassName('msgHere');
for(var i=0; i < el.length; i++) {
clearEle(el[i]);
el[i].appendChild( document.createTextNode(txt[(date2-date1)%txt.length]));
};
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment