fontsize.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. 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. function checkBrowser(){
  22. if (!document.getElementsByTagName){
  23. return true;
  24. }
  25. else{
  26. return false;
  27. }
  28. }
  29. function ndeSetTextSize(chgsize,rs)
  30. {
  31. var startSize;
  32. var newSize;
  33. if (!checkBrowser)
  34. {
  35. return;
  36. }
  37. startSize = parseInt(ndeGetDocTextSize());
  38. if (!startSize)
  39. {
  40. startSize = 16;
  41. }
  42. switch (chgsize)
  43. {
  44. case 'incr':
  45. newSize = startSize + 2;
  46. break;
  47. case 'decr':
  48. newSize = startSize - 2;
  49. break;
  50. case 'reset':
  51. if (rs) {newSize = rs;} else {newSize = 16;}
  52. break;
  53. default:
  54. try{
  55. newSize = parseInt(ndeReadCookie("nde-textsize"));
  56. }
  57. catch(e){
  58. alert(e);
  59. }
  60. if (!newSize || newSize == 'NaN')
  61. {
  62. newSize = startSize;
  63. }
  64. break;
  65. }
  66. if (newSize < 10)
  67. {
  68. newSize = 10;
  69. }
  70. newSize += 'px';
  71. document.getElementsByTagName('html')[0].style.fontSize = newSize;
  72. document.getElementsByTagName('body')[0].style.fontSize = newSize;
  73. ndeCreateCookie("nde-textsize", newSize, 365);
  74. }
  75. function ndeGetDocTextSize()
  76. {
  77. if (!checkBrowser)
  78. {
  79. return 0;
  80. }
  81. var size = 0;
  82. var body = document.getElementsByTagName('body')[0];
  83. if (body.style && body.style.fontSize)
  84. {
  85. size = body.style.fontSize;
  86. }
  87. else if (typeof(getComputedStyle) != 'undefined')
  88. {
  89. size = getComputedStyle(body,'').getPropertyValue('font-size');
  90. }
  91. else if (body.currentStyle)
  92. {
  93. size = body.currentStyle.fontSize;
  94. }
  95. //fix IE bug
  96. if( isNaN(size)){
  97. if(size.substring(size.length-1)=="%"){
  98. return
  99. }
  100. }
  101. return size;
  102. }
  103. function ndeCreateCookie(name,value,days)
  104. {
  105. var cookie = name + "=" + value + ";";
  106. if (days)
  107. {
  108. var date = new Date();
  109. date.setTime(date.getTime()+(days*24*60*60*1000));
  110. cookie += " expires=" + date.toGMTString() + ";";
  111. }
  112. cookie += " path=/";
  113. document.cookie = cookie;
  114. }
  115. function ndeReadCookie(name)
  116. {
  117. var nameEQ = name + "=";
  118. var ca = document.cookie.split(';');
  119. for(var i = 0; i < ca.length; i++)
  120. {
  121. var c = ca[i];
  122. while (c.charAt(0) == ' ')
  123. {
  124. c = c.substring(1, c.length);
  125. }
  126. ctest = c.substring(0,name.length);
  127. if(ctest == name){
  128. return c.substring(nameEQ.length,c.length);
  129. }
  130. }
  131. return null;
  132. }