javascript 在 <body> 部分加载脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6643017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading a script in the <body> section
提问by Bob
I have a javascript for a specific page that I do not wish to be loaded in my header section. Is it possible to load it in the section of the HTML.
我有一个特定页面的 javascript,我不希望在我的标题部分加载它。是否可以在 HTML 部分加载它。
Currently I have all my js code inside the but I want to remove it to a seperate js file that I can load.
目前我在里面有我所有的 js 代码,但我想将它删除到我可以加载的单独 js 文件中。
I tried using this but it did not work.
我尝试使用它,但没有用。
<script type="text/javascript" src="<?php echo base_url();?>js/jquery-1.5.1.min.js"></script>
Thanks
谢谢
回答by Praveen Prasad
Q1: I have a javascript for a specific page that I do not wish to be loaded in my header section. Is it possible to load it in the section of the HTML.
问题 1:我有一个特定页面的 javascript,我不希望在我的页眉部分加载它。是否可以在 HTML 部分加载它。
-Yes you can load javascript any where you want, if writing inline code then make sure you add script tag around your code.
- 是的,您可以在任何地方加载 javascript,如果编写内联代码,请确保在代码周围添加脚本标记。
-also you can request files like in body
- 你也可以请求像正文一样的文件
Q2: Currently I have all my js code inside the but I want to remove it to a seperate js file that I can load.
Q2:目前我有我所有的 js 代码,但我想将它删除到我可以加载的单独 js 文件中。
-- no problem in that, thats even better practice.
——那没问题,那是更好的练习。
Q3Requesting external file
Q3请求外部文件
to request external files you write below written fashion
请求您在以下书面方式下编写的外部文件
<script src="http://file_name.js" type="text/javascript"></script>
回答by T.J. Crowder
It's not only possible (ref), it's frequently a good idea.
这不仅是可能的(ref),而且通常是一个好主意。
Putting your scripts as late in the page as possible, which frequently means just before the closing </body>
tag, means the browser can parse and display your content before stopping to go download your JavaScript file(s) (if external) and fire up the JavaScript interpreter to run the script (inline or external).
将您的脚本尽可能晚地放在页面中,这通常意味着就在结束</body>
标记之前,这意味着浏览器可以在停止下载您的 JavaScript 文件(如果是外部文件)并启动 JavaScript 解释器之前解析并显示您的内容运行脚本(内联或外部)。
Putting scripts "at the bottom" is a fairly standard recommendationfor speeding up the apparent load time of your page.
将脚本放在“底部”是加快页面明显加载时间的相当标准的建议。
回答by Ibu
Yes it is possible. Try and see.
对的,这是可能的。试试看。
For debugging, hardcode the jquery full path.
对于调试,硬编码 jquery 完整路径。
It is sometime recommended to load it at the end of the of the body, to make the main content of the page load faster.
有时建议在正文的末尾加载它,以使页面的主要内容加载速度更快。
回答by Saeed Neamati
Yahoo engineers recommendation for higher performance is to include your scripts at the end of your HTML, just before </body>
tag. Therefore, it's even better.
雅虎工程师推荐的更高性能是将您的脚本包含在 HTML 的末尾,就在</body>
标记之前。因此,它甚至更好。
To see where the problem is, you gotta first make sure that your js file is loading. User Firebugand go to scriptstab. Do you see your script? If not, then something is wrong with your path.
要查看问题出在哪里,您首先要确保您的 js 文件正在加载。用户Firebug并转到脚本选项卡。你看到你的脚本了吗?如果没有,那么你的路径有问题。
回答by Variant
it should work...
它应该工作...
Did you try to view the generated source and see if the PHP code indeed generated the right path?
您是否尝试查看生成的源代码并查看 PHP 代码是否确实生成了正确的路径?
beside that, it is recommended to load jQuery from a CDN such as google's : https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
除此之外,建议从 CDN 加载 jQuery,例如 google 的:https: //ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
回答by Quentin
Is it possible to load it in the section of the HTML.
是否可以在 HTML 部分加载它。
Yes.
是的。
From the spec:
从规范:
<!ELEMENT BODY O O (%block;|SCRIPT)+ +(INS|DEL) -- document body -->
SCRIPT is among the elements that may be a child of the BODY elements. Numerous other elements may also have SCRIPT children.
SCRIPT 是可能是 BODY 元素的子元素的元素之一。许多其他元素也可能有 SCRIPT 子元素。
<script type="text/javascript" src="<?php echo base_url();?>js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>js/jquery-1.5.1.min.js"></script>
When I run echo base_url()
I get my the hostname of my server. This would result in a URL such as example.comjs/query-1.5.1.min.js. You probably should drop that PHP snippet entirely and just use: src="/js/jquery-1.5.1.min.js"
which would resolve to http://example.com/s/query-1.5.1.min.js.
当我运行时,echo base_url()
我得到了服务器的主机名。这将产生一个 URL,例如example.comjs/query-1.5.1.min.js。你或许应该丢弃PHP完全,只是用片段:src="/js/jquery-1.5.1.min.js"
这将解析HTTP://example.com /S /查询1.5.1.min.js。