首页 文章

grab 点击并存储它直到在JS页面上加载目标脚本

提问于
浏览
0

我有 onclick 功能

<a onclick="func()"></a>

在主页面上,用户可以在该页面上加载所有脚本文件之前单击此链接,这样他们就会“空”点击它,他们太早按下按钮 .

Note: I'm not looking for how to load the script faster/in sync with the main page.

我的问题是如何"catch"这次点击和"store"只要它加载目标脚本然后继续 click .

我认为.promise()可以做到这一点,但我以前从未使用它,有人能指出我正确的方向吗?

1 回答

  • 0

    在页面的开头,定义一个数组,您可以使用 push() 方法存储所有点击 . 然后在加载js文件时处理它们 .

    <!-- HTML PAGE -->
    <script>var clicks = [];</script>
    
    <a href="#" onclick="clicks.push([func, this])">AAA</a>
    <a href="#" onclick="clicks.push([func, this])">BBB</a>
    <a href="#" onclick="clicks.push([func, this])">CCC</a>
    
    <!-- JAVASCRIPT FILE  -->
    <script>
        // The function executed by clicking on a link
        function func(node) {
            console.log(node.innerHTML);
        }
    
        window.onload = function () {
            // When the page is loaded, rewrite the push() method
            // So, when you click on a link, it should execute the function instantly
            clicks.push = function (click) {
                var fn = click[0],
                    node = click[1];
                fn.call(this, node);
            };
    
            // Execute all saved clicks
            for (var c = 0; c < clicks.length; c++) {
                clicks.push(clicks[c]);
            }
        };
    </script>
    

相关问题