js implements an approach that does not repeat imports

  • 2021-01-19 22:02:01
  • OfStack

This article shows an example of how js implements non-repeat imports. To share with you for your reference, as follows:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title> No title page </title>
  <mce:script src="jquery-1.4.4.js" mce_src="jquery-1.4.4.js" type="text/javascript"></mce:script>
  <mce:script type="text/javascript"><!--
    function importOnce(scriptPath) {
      var scripts = document.getElementsByTagName("script");
      for (var index = 0; index < scripts.length; index++) {
        if (scripts[index].src.lastIndexOf(scriptPath) != -1) {
          return;
        }
      }
      var scriptTag = document.createElement("script");
      scriptTag.type = "text/javascript";
      scriptTag.src = scriptPath;
      var headTag = document.getElementsByTagName("head")[0];
      headTag.appendChild(scriptTag);
    }
    importOnce("jquery-1.4.4.js");
    importOnce("jquery-1.4.4.js");
    importOnce("jquery-1.4.3.js");
    importOnce("jquery-1.4.3.js");
// --></mce:script>
</head>
<body>
</body>
</html>

More about JavaScript related content interested readers can view the site features: "JavaScript search algorithm skills summary", "JavaScript animation effects and skills summary", "JavaScript error and debugging skills summary", "JavaScript data structure and algorithm skills summary", "JavaScript eraser algorithm and skills summary" and "JavaScript mathematical operation usage summary"

I hope this article is helpful to JavaScript program design.


Related articles: