JAVASCRIPT   58

Fontsize js

Guest on 18th July 2022 02:05:20 AM

  1. /*
  2. * Copyright  The Apache Software Foundation or its licensors,
  3. * as applicable.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. *     http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. function init()
  18. { //embedded in the doc
  19.   //ndeSetTextSize();
  20. }
  21.  
  22. function checkBrowser(){
  23.   if (!document.getElementsByTagName){
  24.     return true;
  25.   }
  26.   else{
  27.     return false;
  28.   }
  29. }
  30.  
  31.  
  32. function ndeSetTextSize(chgsize,rs)
  33. {
  34.   var startSize;
  35.   var newSize;
  36.  
  37.   if (!checkBrowser)
  38.   {
  39.     return;
  40.   }
  41.  
  42.   startSize = parseInt(ndeGetDocTextSize());
  43.  
  44.   if (!startSize)
  45.   {
  46.     startSize = 16;
  47.   }
  48.  
  49.   switch (chgsize)
  50.   {
  51.   case 'incr':
  52.     newSize = startSize + 2;
  53.     break;
  54.  
  55.   case 'decr':
  56.     newSize = startSize - 2;
  57.     break;
  58.  
  59.   case 'reset':
  60.     if (rs) {newSize = rs;} else {newSize = 16;}
  61.     break;
  62.  
  63.   default:
  64.     try{
  65.       newSize = parseInt(ndeReadCookie("nde-textsize"));
  66.     }
  67.     catch(e){
  68.       alert(e);
  69.     }
  70.    
  71.     if (!newSize || newSize == 'NaN')
  72.     {
  73.       newSize = startSize;
  74.     }
  75.     break;
  76.  
  77.   }
  78.  
  79.   if (newSize < 10)
  80.   {
  81.     newSize = 10;
  82.   }
  83.  
  84.   newSize += 'px';
  85.  
  86.   document.getElementsByTagName('html')[0].style.fontSize = newSize;
  87.   document.getElementsByTagName('body')[0].style.fontSize = newSize;
  88.  
  89.   ndeCreateCookie("nde-textsize", newSize, 365);
  90. }
  91.  
  92. function ndeGetDocTextSize()
  93. {
  94.   if (!checkBrowser)
  95.   {
  96.     return 0;
  97.   }
  98.  
  99.   var size = 0;
  100.   var body = document.getElementsByTagName('body')[0];
  101.  
  102.   if (body.style && body.style.fontSize)
  103.   {
  104.     size = body.style.fontSize;
  105.   }
  106.   else if (typeof(getComputedStyle) != 'undefined')
  107.   {
  108.     size = getComputedStyle(body,'').getPropertyValue('font-size');
  109.   }
  110.   else if (body.currentStyle)
  111.   {
  112.    size = body.currentStyle.fontSize;
  113.   }
  114.  
  115.   //fix IE bug
  116.   if( isNaN(size)){
  117.     if(size.substring(size.length-1)=="%"){
  118.       return
  119.     }
  120.  
  121.   }
  122.  
  123.   return size;
  124.  
  125. }
  126.  
  127.  
  128.  
  129. function ndeCreateCookie(name,value,days)
  130. {
  131.   var cookie = name + "=" + value + ";";
  132.  
  133.   if (days)
  134.   {
  135.     var date = new Date();
  136.     date.setTime(date.getTime()+(days*24*60*60*1000));
  137.     cookie += " expires=" + date.toGMTString() + ";";
  138.   }
  139.   cookie += " path=/";
  140.  
  141.   document.cookie = cookie;
  142.  
  143. }
  144.  
  145. function ndeReadCookie(name)
  146. {
  147.   var nameEQ = name + "=";
  148.   var ca = document.cookie.split(';');
  149.  
  150.  
  151.   for(var i = 0; i < ca.length; i++)
  152.   {
  153.     var c = ca[i];
  154.     while (c.charAt(0) == ' ')
  155.     {
  156.       c = c.substring(1, c.length);
  157.     }
  158.  
  159.     ctest = c.substring(0,name.length);
  160.  
  161.     if(ctest == name){
  162.       return c.substring(nameEQ.length,c.length);
  163.     }
  164.   }
  165.   return null;
  166. }

Raw Paste


Login or Register to edit or fork this paste. It's free.