将身体中的JavaScript文件链接到标题中会导致问题? [英] Will linking javascript files in the body rather than in the header cause a problem?

查看:91
本文介绍了将身体中的JavaScript文件链接到标题中会导致问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试做的

<script type="text/javascript" src="resources/application.js"></script>
    <script type="text/javascript" >
       $(document).ready(createHeader()); 
       $(document).ready(scriptSet()); 
    </script>

id,以避免不必分开两个,而一般来说,我只看到脚本链接document.ready函数在放在那里似乎不起作用。但是,一旦放置在身体的末端,一切似乎都可以正常工作,所以这会导致任何问题,还是这样吗?

id like to avoid having to separate the two, and while generally i see script links only inside the header the document.ready functions dont seem to work when put there. However, everything seems to work completely fine when placed at the end of the body, so would this cause any problems or is this fine?

推荐答案

在功能上,只要将代码包含在 $(document).ready(function(){}); 之内来自 jQuery文件的之后,如果它位于 >体。 $(document).ready 确保在执行任何脚本之前DOM完全加载。

Functionally, as long as you enclose your code inside a $(document).ready(function(){ }); and it comes after the jQuery file includes, it does not matter if it's in the head or the body. $(document).ready ensures that the DOM is fully loaded before any script is executed.

无论如何,将您的脚本包含的所有脚本和脚本放在正文的底部是最佳以加载性能。

HOWEVER, putting all of your script includes and scripts at the bottom of the body is best for loading performance.

本文解释

This article explains it nicely.

示例:

        <body>

    <!-- MY HTML CODE -->

    <!-- START javascript -->
        <script type="text/javascript" src="/javascript/jquery/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="/javascript/jquery/plugins/jquery.random_plugin.js"></script>
        <script type="text/javascript" src="/javascript/jquery/plugins/jquery.random_plugin2.js"></script>
        <script type="text/javascript" src="/javascript/some_other_scripts.js"></script>

        <script type="text/javascript" language="JavaScript">
        //<![CDATA[
            $(document).ready(function(){
                // my code
            });
        //]]>
        </script>
    <!-- END javascript -->

        </body>

这篇关于将身体中的JavaScript文件链接到标题中会导致问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆