我应该将 Javascript 放在 HTML 文件的头部还是正文中? [英] Should I Put Javascript in the Head or Body of an HTML File?

查看:31
本文介绍了我应该将 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.

推荐答案

将它放在文档的 之前的

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