亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - How to load a js file before opening the website?
曾經(jīng)蠟筆沒(méi)有小新
曾經(jīng)蠟筆沒(méi)有小新 2017-05-31 10:34:00
0
4
759

And after the website is loaded and opened normally, the code for loading js will not be displayed in the web page code. Can this be done? ? ? Solve

曾經(jīng)蠟筆沒(méi)有小新
曾經(jīng)蠟筆沒(méi)有小新

reply all(4)
大家講道理

I don’t know how the questioner defines it before the website is opened?

If you just want the web page to load js files earlier, you can put the script reference in the head tag.
However, the question also requires that the web page code does not display the js after opening. This is how I understand it. In this case, there are two main steps:

  1. Write an inline js code in the head tag. The main function of the code is to introduce the js file and follow-up actions

  2. The so-called follow-up action is to trigger the loading event after introducing the file to remove the introduction of the file from the document dom

I will simply list a demo:

First prepare the page t.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>waterfall</title>
    <script type="text/javascript">
        (function(){
            // 動(dòng)態(tài)加載script
            var script = document.createElement('script');
            var head = document.getElementsByTagName('head')[0];
            // console.log(head);
            // 腳本加載完就移除
            script.onload = function(){
                console.log('loaded script ready to remove');
                head.removeChild(script);
            };
            script.src = './t.js';
            console.log('load script');
            // 將script放到head里
            head.appendChild(script);
        })();
    </script>
</head>
<body>
    <p id="wrap" class="wrap"></p>
</body>
</html>

Introduced js: t.js

(function(){
    console.log('hello i am t');
})()

I wonder if this is the main effect of the question?

左手右手慢動(dòng)作

If you want to improve the loading speed, you can use cache to load js in advance. I have never encountered this before

世界只因有你

Use a web server (such as nginx) to insert content into the page?
Then this js is also monitoring whether the page has been loaded, and deletes itself after loading.

Ty80

Try putting the actual web page in the iframe and js outside the iframe?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template