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.
 
 
 

48 lines
1.6 KiB

  1. function DynLoadFile(filename)
  2. {
  3. var filetype = filename.match(/\.[a-zA-Z]+$/);
  4. //if filename is a external JavaScript file
  5. if (filetype == ".js")
  6. {
  7. var fileref = document.createElement('script');
  8. fileref.setAttribute("type","text/javascript");
  9. fileref.setAttribute("src", filename);
  10. }
  11. //if filename is an external CSS file
  12. else if (filetype == ".css")
  13. {
  14. var fileref = document.createElement("link");
  15. fileref.setAttribute("rel", "stylesheet");
  16. fileref.setAttribute("type", "text/css");
  17. fileref.setAttribute("href", filename);
  18. }
  19. if (fileref)
  20. document.getElementsByTagName("head")[0].appendChild(fileref);
  21. }
  22. function DynRemoveFile(filename)
  23. {
  24. var filetype = filename.match(/\.[a-zA-Z]+$/);
  25. var targetelement = "none";
  26. var targetattr = "none";
  27. //Determine element type to create nodelist from and corresponding attribute to test for
  28. if (filetype==".js")
  29. {
  30. targetelement = "script";
  31. targetattr = "src";
  32. }
  33. else if (filetype == ".css")
  34. {
  35. targetelement = "link";
  36. targetattr = "href";
  37. }
  38. var allsuspects = document.getElementsByTagName(targetelement);
  39. //Search backwards within nodelist for matching elements to remove
  40. for (var i = allsuspects.length; i >= 0; i--)
  41. if (allsuspects[i] &&
  42. allsuspects[i].getAttribute(targetattr) &&
  43. allsuspects[i].getAttribute(targetattr).indexOf(filename) != -1)
  44. allsuspects[i].parentNode.removeChild(allsuspects[i]); //remove element by calling parentNode.removeChild()
  45. }