在Doctype声明之前添加脚本标记 [英] Adding the script tag before Doctype Declaration

查看:85
本文介绍了在Doctype声明之前添加脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚做了一点测试,如果我按照下面说明的方式添加脚本,它似乎在大多数现代主流浏览器中执行。它在页面加载之前执行,我想知道这是否适用于所有浏览器(包括历史性的)?

I just did a little test and if I add a script in the manner illustrated below it seems to execute in most modern major browsers. It executes before the page is loaded, I was wondering if this would work across all browsers (including historic)?

<script type="text/javascript">
alert("hello world");
</script>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
</body>
</html>

我当然试图找到一种方法来执行脚本以在任何之前设置页面它被加载...对此目的的任何输入将不胜感激。使用这种方法会有误吗?

I am of course trying to find a way to execute a script to set a page up before any of it is loaded...any input towards this end would be greatly appreciated. Would it be wrong to use this method?

提前感谢您的帮助!

推荐答案

脚本被执行,但是标记(DOCTYPE字符串之前的任何元素)将一些浏览放到怪癖模式,这意味着大量记录不完整的怪癖和怪异,以及混乱。

The script gets executed, but the the markup (any element before a DOCTYPE string) puts some browses to quirks mode, which means a large number of poorly documented quirks and oddities, and mess, too.

所以无论如何你的理由是,你至少应该把元素放在第一个语法正确的位置,即在< head> 标签之后。与将其放在文档开头相比,这几乎无关紧要。

So whatever your reasons are, you should at least put the element in the first syntactically correct place, namely right after the <head> tag. This can hardly matter as compared with placing it at the start of the document.

展示位置是否解决了您的真实问题是完全不同的事情。你应该问一个单独的问题,清楚地描述问题(而不是一个假设的解决方案),最好用一些证明问题的代码说明。

Whether the placement solves your real problem is an entirely different thing. You should ask a separate question, clearly describing the problem (rather than an assumed solution), preferably illustrated by some code that demonstrates the problem.

这篇关于在Doctype声明之前添加脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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