我应该将 Javascript 放在 HTML 文件的头部还是正文中? [英] Should I Put Javascript in the Head or Body of an HTML File?
本文介绍了我应该将 Javascript 放在 HTML 文件的头部还是正文中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在制作一个搜索引擎,当用户访问我的页面时,我有将 http 重定向到 https 的代码.
除了我不确定是否应该把它放在头部 或我页面的 body 部分.
这是我所拥有的:
I am making a search engine, and I have the code to redirect http to https when users access my page.
Except I'm not sure if I should put it in the head or body section of my page.
Here's what I've got:
if(window.location.protocol != 'https:') {
location.href = location.href.replace("http://", "https://");
}
另外我想知道我的代码是否真的有效.
Also I would like to know if the code I have actually works if that's OK.
推荐答案
将它放在文档的 之前的
标签中任何其他
标记,以便在下载页面的所有资源之前执行.
Put it inside a <script>
tag in the <head>
of your document before any other <script>
tags, so that it will execute before downloading all the resources for the page.
这篇关于我应该将 Javascript 放在 HTML 文件的头部还是正文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文