You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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