Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

132 rader
3.7 KiB

  1. //-----------------------------------------------------------------------------
  2. // COOKIE MANAGEMENT - nom nom nom nom -
  3. //-----------------------------------------------------------------------------
  4. function LolCookie() { }
  5. function LolCookieDays(m_name, m_value, m_expire_days)
  6. {
  7. var cookie = new LolCookie();
  8. cookie.m_name = m_name;
  9. cookie.m_value = m_value;
  10. cookie.m_expire_date = new Date();
  11. cookie.m_expire_date.setDate(cookie.m_expire_date.getDate() + m_expire_days);
  12. return cookie;
  13. }
  14. function LolCookieDate(m_name, m_value, m_expire_date)
  15. {
  16. var cookie = new LolCookie();
  17. cookie.m_name = m_name;
  18. cookie.m_value = m_value;
  19. cookie.m_expire_date = m_expire_date;
  20. return cookie;
  21. }
  22. //Set a cookie
  23. function StoreLolCookie(cookie)
  24. {
  25. GetLolCookie(cookie.m_name, true);
  26. var enc_value = escape(cookie.m_value) + ";";
  27. //Add expire days
  28. if (cookie.m_expire_date)
  29. enc_value += " expires=" + cookie.m_expire_date.toUTCString() + ";";
  30. enc_value = cookie.m_name + "=" + enc_value;
  31. //Store cookies
  32. document.cookie = enc_value;
  33. }
  34. //Get a cookie
  35. function GetLolCookie(name, remove)
  36. {
  37. var cki_doc = document.cookie;
  38. var cookie = null;
  39. var cki_start = cki_doc.indexOf(name + "=");
  40. if (cki_start > -1)
  41. {
  42. cookie = new LolCookie();
  43. cookie.m_name = name;
  44. cookie.m_expire_date = null;
  45. //Retrieve value
  46. var val_start = cki_doc.indexOf("=", cki_start) + 1;
  47. var val_end = cki_doc.indexOf(";", cki_start);
  48. if (val_end < 0)
  49. val_end = cki_doc.length;
  50. cookie.m_value = unescape(cki_doc.substring(val_start, val_end));
  51. if (remove)
  52. document.cookie = name + "=; expires=0;";
  53. }
  54. return cookie;
  55. }
  56. //Check the existence of a cookie
  57. function DoesLolCookieExist(name)
  58. {
  59. var cki_doc = document.cookie;
  60. if (cki_doc.indexOf(name + "=") > -1)
  61. return true;
  62. return false;
  63. }
  64. //-----------------------------------------------------------------------------
  65. // FILE MANAGEMENT
  66. //-----------------------------------------------------------------------------
  67. //Dynamic load
  68. function DynLoadFile(filename)
  69. {
  70. var filetype = filename.match(/\.[a-zA-Z]+$/);
  71. //if filename is a external JavaScript file
  72. if (filetype == ".js")
  73. {
  74. var fileref = document.createElement('script');
  75. fileref.setAttribute("type","text/javascript");
  76. fileref.setAttribute("src", filename);
  77. }
  78. //if filename is an external CSS file
  79. else if (filetype == ".css")
  80. {
  81. var fileref = document.createElement("link");
  82. fileref.setAttribute("rel", "stylesheet");
  83. fileref.setAttribute("type", "text/css");
  84. fileref.setAttribute("href", filename);
  85. }
  86. if (fileref)
  87. document.getElementsByTagName("head")[0].appendChild(fileref);
  88. }
  89. //Dynamic remove
  90. function DynRemoveFile(filename)
  91. {
  92. var filetype = filename.match(/\.[a-zA-Z]+$/);
  93. var targetelement = "none";
  94. var targetattr = "none";
  95. //Determine element type to create nodelist from and corresponding attribute to test for
  96. if (filetype==".js")
  97. {
  98. targetelement = "script";
  99. targetattr = "src";
  100. }
  101. else if (filetype == ".css")
  102. {
  103. targetelement = "link";
  104. targetattr = "href";
  105. }
  106. var allsuspects = document.getElementsByTagName(targetelement);
  107. //Search backwards within nodelist for matching elements to remove
  108. for (var i = allsuspects.length; i >= 0; i--)
  109. if (allsuspects[i] &&
  110. allsuspects[i].getAttribute(targetattr) &&
  111. allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1)
  112. allsuspects[i].parentNode.removeChild(allsuspects[i]); //remove element by calling parentNode.removeChild()
  113. }