Skip to content

Instantly share code, notes, and snippets.

@fedmich
Created April 9, 2012 09:01
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 fedmich/2342421 to your computer and use it in GitHub Desktop.
Save fedmich/2342421 to your computer and use it in GitHub Desktop.
Javascript Codes
function doc_append ( obj ){
if( document.getElementsByTagName("head").length > 0 ){
document.getElementsByTagName("head")[0].appendChild( obj );
}
else{
document.getElementsByTagName("body")[0].appendChild( obj );
}
}
var url_css = 'http://example.com/css/file.css';
var o_css = document.createElement("link");
o_css.setAttribute("rel", "stylesheet");
o_css.setAttribute("type", "text/css");
o_css.setAttribute("href", url_css + '?r=' + (Math.random()*99999999) );
doc_append( o_css );
var url_js = 'http://example.com/js/file.js';
var o_js = document.createElement("script");
o_js.setAttribute("src", url_js + '?r=' + (Math.random()*99999999) );
doc_append( o_js );
function gup( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ){
return "";
} else{
return results[1];
}
}
(function() {
var imgs = document.getElementsByTagName('img');
var imgs_no_alt = [];
for (var i = 0; i < imgs.length; i++) {
if( ! imgs[ i ].alt ){
imgs_no_alt.push( imgs[i] );
}
}
if( imgs_no_alt.length ){
console.log("The following images don't have alt attributes.");
for (var i = 0; i < imgs_no_alt.length; i++) {
console.log( imgs_no_alt[i] );
}
}
else{
console.log('All images have alt attributes.');
}
})();
function img_AspectRatio ( width, height, max_width, max_height) {
var ratio = Math.min( max_width / width, max_height / height);
return { width:(width * ratio).toFixed(0), height:( height * ratio).toFixed(0) };
}
function inArray(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment