Skip to content

Instantly share code, notes, and snippets.

@reneruiz

reneruiz/ajax.js Secret

Created May 29, 2013 02:00
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 reneruiz/0fba9f66049c5c8e51b4 to your computer and use it in GitHub Desktop.
Save reneruiz/0fba9f66049c5c8e51b4 to your computer and use it in GitHub Desktop.
preventScroll not working reliabily
var ajax = (function() {
// Private
var httpRequest, data,
ajaxState = {
isDone : 4,
isOkay : 200
},
successHandler = function(data) {
var json = JSON.parse(data);
getRandomKey(json);
},
errorHandler = function() {
console.log("Error: Status is not reporting 'Okay'");
},
getRandomKey = function(array) {
var totalKeys = array.length;
if (totalKeys > 0) {
var randomKey = random(totalKeys);
}
templify(array[randomKey], ".headline");
},
templify = function(entry, element) {
var container = document.querySelector(element);
container.innerHTML = entry;
},
random = function(int) {
return Math.floor(Math.random() * int);
};
// Public
var makeRequest = function(method, url) {
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
httpRequest.open(method, url, true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === ajaxState.isDone) {
if (httpRequest.status === ajaxState.isOkay) {
data = httpRequest.responseText;
successHandler(data);
} else {
errorHandler();
}
}
};
httpRequest.send();
};
return {
request: makeRequest
};
})();
var preventScroll = (function() {
var resize = function(el) {
var element = document.querySelector(el),
style = window.getComputedStyle(element), // element.style.fontSize doesn't work
fontsize = parseFloat(style.fontSize);
// console.log(document.body.scrollHeight + " > " window.innerHeight);
while (document.body.scrollHeight > window.innerHeight) {
fontsize = fontsize - 1; // For some reason 'fontsize--;' doesn't work
element.style.fontSize = fontsize + 'px';
}
};
return {
resize: resize
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment