-
Notifications
You must be signed in to change notification settings - Fork 2
/
fontSwitcher_v3_0.min.js
1 lines (1 loc) · 2.62 KB
/
fontSwitcher_v3_0.min.js
1
function fontSwitcher(fontInput, classInput){function checkLink(thisFont){var linkName="fSg_"+thisFont;if(!(document.getElementById(linkName))){var linkElement=document.createElement("link");linkElement.id=linkName;linkElement.rel="stylesheet";linkElement.type="text/css";document.head.appendChild(linkElement);document.getElementById(linkName).href=makeGoogleURL(thisFont);}else if(!((document.getElementById(linkName).rel=="stylesheet")&&(document.getElementById(linkName).type=="text/css"))) {document.getElementById(linkName).rel="stylesheet";document.getElementById(linkName).type="text/css";}}function checkScript(thisFont){var scriptName="fSa_"+thisFont;if (!(document.getElementById(scriptName))){var scriptElement=document.createElement("script");scriptElement.id=scriptName;scriptElement.type="text/javascript";document.head.appendChild(scriptElement);document.getElementById(scriptName).src=makeAdobeURL(thisFont);}}function makeGoogleURL(fontName){var newFontURL="https://fonts.googleapis.com/css?family="+addReplChar(fontName, '+');return newFontURL;}function makeAdobeURL(fontName){var newFontURL="//use.edgefonts.net/"+addReplChar(fontName, '-')+".js";return newFontURL;}function addReplChar(fName, replChar){var urlString='';for(var i=0;i<(fName.length);i++){if(fName.charAt(i)==" "){urlString=urlString+replChar;}else{urlString=urlString+fName.charAt(i);}}return urlString;}function checkIfArray(paramToCheck){var testString=''+paramToCheck.constructor+'';if(!!(arrayPatt.test(testString))){return true;}else{return false;}}function getStringFromArray(arrayParam){var tempString='';for (var i=0; i<arrayParam.length;i++){if(i>0){tempString=tempString+',';}tempString=tempString+arrayParam[i];}return tempString;}function loopThroughFontArray(){var flagVar;for(var i=0; i<fontArray.length;i++){if (!!(/#/.test(fontArray[i]))){flagVar=fontArray[i].slice(-2).toLowerCase();fontArray[i]=fontArray[i].slice(0,-2);switch(flagVar){case('#g'):checkLink(fontArray[i]);break;case('#a'):fontArray[i]=fontArray[i].toLowerCase();checkScript(fontArray[i]);break;}}}}var fontStack='';var fontArray=[];var arrayPatt=/\bArray/;var elements;if(!!(checkIfArray(fontInput))){fontArray=fontInput;}else{fontArray[0]=fontInput;}loopThroughFontArray();fontStack=getStringFromArray(fontArray);if(classInput[0]=='#'){document.getElementById(classInput.slice(1)).style.fontFamily=fontStack;}else if (classInput[0]=='.'){elements=document.getElementsByClassName(classInput.slice(1));for(var a=0;a<elements.length;a++){elements[a].style.fontFamily=fontStack;}}else{elements=document.getElementsByTagName(classInput);for(var a=0;a<elements.length;a++){elements[a].style.fontFamily=fontStack;}}}