简体   繁体   English

网页加载失败:JavaScript过多?

[英]Hang on Page Load: Too much Javascript?

I've finally fixed some of my Javascript issues, and managed to use only one library now (it was crazy before). 我终于解决了一些Javascript问题,并且现在只使用了一个库(以前很疯狂)。

There's a little bit of a hang in the page load, so I was going to see if you guys noticed anything I could make more efficient in my scripting. 页面加载有点挂起,所以我要看看你们是否注意到我可以提高脚本编写效率的任何事情。 It's a little all over the place, so I might have some unnecessary functions. 它到处都是,所以我可能有一些不必要的功能。 Suggestions? 有什么建议吗?

Thanks in advance! 提前致谢!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="player/src/jquery.ubaplayer.js"></script>
<script>
$(document).ready(function() { 
    $("#ubaPlayer").ubaPlayer({
        codecs: [{name:"MP3", codec: 'audio/mpeg;'}]                
    });

    $('a[class=video]').click(function () {
        if ($("#ubaPlayer").ubaPlayer("playing") === true) {
            $("#ubaPlayer").ubaPlayer("pause");
        }
        return false;
    });
})
</script>
<script type="text/javascript" src="/fancybox/source/jquery.fancybox.pack.js?v=2.1.4">                </script>
<script type="text/javascript" src="/fancybox/source/helpers/jquery.fancybox-media.js?v=1.0.5"></script>
<script type="text/javascript">
jQuery(document).ready(function() {

$(".video").click(function() {
    $.fancybox({
        'padding'       : 0,
        'autoScale'     : false,
        'transitionIn'  : 'none',
        'transitionOut' : 'none',
        'title'         : this.title,
        'width'         : 640,
        'height'        : 385,
        'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
        'type'          : 'swf',
        'swf'           : {
        'wmode'             : 'transparent',
        'allowfullscreen'   : 'true'
        }
    });

    return false;
});
});
</script>

</head>

In this code snippet alone you have four external scripts and two inline script blocks. 仅在此代码段中,您就有四个外部脚本和两个内联脚本块。

Minimizing the number of HTTP requests happens to be the golden rule of web performance. 最小化HTTP请求数量恰好是Web性能的黄金法则。 If possible, try combining the external scripts into one or two JS files. 如果可能,请尝试将外部脚本组合到一个或两个JS文件中。

The other curious thing is that your inline script blocks both bind functions to jQuery's .ready() . 另一个奇怪的是,您的内联脚本块都将两个绑定函数都绑定到jQuery的.ready() Why not have one function that does both things? 为什么没有一个功能可以同时做两件事? See below for an example: 参见以下示例:

$(document).ready(function() { 
    $("#ubaPlayer").ubaPlayer({
        codecs: [{name:"MP3", codec: 'audio/mpeg;'}]                
    });

    $('a[class=video]').click(function () {
        if ($("#ubaPlayer").ubaPlayer("playing") === true) {
            $("#ubaPlayer").ubaPlayer("pause");
        }
        return false;
    });

    $(".video").click(function() {
        $.fancybox({
            'padding'       : 0,
            'autoScale'     : false,
            'transitionIn'  : 'none',
            'transitionOut' : 'none',
            'title'         : this.title,
            'width'         : 640,
            'height'        : 385,
            'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type'          : 'swf',
            'swf'           : {
                'wmode'             : 'transparent',
                'allowfullscreen'   : 'true'
            }
        });

        return false;
    });
})

The other part that I notice is that you have a </head> tag below the JavaScript. 我注意到的另一部分是您在JavaScript下方有一个</head>标记。 I've said this before to the ire of others, but I would strongly recommend moving the scripts to just before the </body> tag instead. 我之前曾对其他人感到愤怒,但我强烈建议将脚本移到</body>标记之前。 The page load is affected by script loading and parsing, so if you can defer that to later, that would be better to perceived page load at least. 页面加载受脚本加载和解析的影响,因此,如果您可以将其推迟到以后,那么至少可以更好地感知页面加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM