代码之家  ›  专栏  ›  技术社区  ›  Rye

执行*。如果月份是十二月

  •  0
  • Rye  · 技术社区  · 3 年前

    我正在尝试制作这个脚本

    <script>
    $(document).ready(function(){
        var d = new Date();
        n = d.getMonth();
    
        if (n == 11) {
           src="extrafiles/effect/snow.js";
        }
    });
    </script>
    

    思想?

    编辑:

    如果(d.getMonth()。比赛(11){ //一定要叫雪。js; }

    <script>
    var currentTime = new Date();
    var month = currentTime.getMonth() + 1;
    var total = month;
    if (total == 12 || total == 1 || total == 2)
    {
    //beginning of script
    START JS FILE VIA ???
    //end of script
    }
    </script>
    
    2 回复  |  直到 3 年前
        1
  •  1
  •   epascarello    3 年前

    您可以使用文档。编写以添加脚本

    if (new Date().getMonth()===11) document.write('<script src="https://ry3yr.github.io/OSTR/myhomepage/snow.js"><\/script>');
        2
  •  1
  •   Arnaud Dev    3 年前

    试着这样做:

      if (n === 11) {
        let myScript = document.createElement("script");
        myScript.setAttribute("src", "https://www.example.com/snow.js");
        document.body.appendChild(myScript);
        makeSnow();
      }
    

    或在ES6模式下:

    <!doctype html>
    <script>
      async function load() {
        let say = await import('./snow.js');
        say.makeSnow();
    
      }
    </script>
    <button onclick="load()">Click me</button>
    
        3
  •  1
  •   Cjmarkham    3 年前

    你可以利用 import 根据您的条件导入文件。您也可以导入该文件,只需在条件语句中从该文件调用函数。

    <DOCTYPE html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
          $(async () => { // same as $(document).ready(async function(){
            const month = new Date().getMonth()
            if (month === 11) {
              const effect = await import('./snow.js')
              effect.run()
            }
          })
        </script>
    </head>
    <body>
    
    </body>

    <DOCTYPE html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script>
          const effect = await import('./snow.js')
          
          $(async () => { 
            const month = new Date().getMonth()
            if (month === 11) {
              effect.run()
            }
          })
        </script>
    </head>
    <body>
    
    </body>

    请注意,这将在堆栈溢出时引发错误,因为该文件不存在,但我已验证它在本地工作。

    既然您已经显示了该文件,我可以看到它实际上并没有导出任何这样的内容

    <DOCTYPE html>
    <head>
    </head>
    <body>
      <script>
          document.addEventListener('DOMContentLoaded', () => {
            const s = document.createElement('script')
            s.type = 'text/javascript'
            s.src = 'https://ry3yr.github.io/OSTR/myhomepage/snow.js'
            document.body.appendChild(s)
          })
      </script>
    </body>