Thursday, December 8, 2016

How many scripts?

How many pieces of script are running on a page?

I counted them in the HTML Source and in the DOM running the script below in the console for each website.

The number of external Javascript files is from the Console > Network > JS



(function() {
function getSource(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
return callback(xhr.responseText);
}
};
xhr.open('GET', url, true);
xhr.send(null);
}
function countScripts(p) {
var count = (p.match(/<script/g) || []).length;
return (count);
}
var url = document.location.href;
getSource(url, function(source) {
console.log(url + "\n\tScripts in source: " + countScripts(source) + "\n\tScripts in DOM: " + document.getElementsByTagName("script").length);
});
})();

No comments :

Post a Comment