admin管理员组文章数量:1313106
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?
Thanks in advance for any help!
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?
Thanks in advance for any help!
Share Improve this question edited Nov 24, 2013 at 15:42 user1360809 asked Nov 24, 2013 at 15:33 user1360809user1360809 7452 gold badges13 silver badges25 bronze badges 9-
1
What problem are you trying to solve? (And why are you using an XHTML doctype and
<html>
tag and then declaring the content type to be HTML?) – Pointy Commented Nov 24, 2013 at 15:35 - 2 This will drop IEs to Quirks mode, I doubt you want to run your pages in IE5.5 : ). – Teemu Commented Nov 24, 2013 at 15:39
- 1 @Pointy: Because nobody was taught the right content-type for XHTML, and even if everyone were taught it they couldn't use the knowledge years ago because IE didn't start supporting it until version 9. – BoltClock Commented Nov 24, 2013 at 15:39
- 2 @user1360809 Well this won't solve that problem. Because the document won't exist when the code runs, your code won't be able to gain access to any of the page elements. – Pointy Commented Nov 24, 2013 at 15:42
- 1 The answer is short: don't do that. – Shadow Wizzard Commented Nov 24, 2013 at 15:51
4 Answers
Reset to default 5The 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.
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 pared 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.
According to the specs,
A conformant document in the HTML syntax must consist of the following parts, in the following order:
- Optionally, a single U+FEFF BYTE ORDER MARK (BOM) character.
- Any number of ments and space characters.
- A doctype.
- Any number of ments and space characters.
- An html element, with its attributes (if any) and its contents (if any).
Browsers follow these specs, and your code (even though works now) may break in the future, since it clearly breaks the rule of order of elements.
Secondly, it's almost always better to load the scripts last for performance gain.
You mention in the ments that you want to hide/show elements before the page is displayed and that onload
is too slow. Try using the DOMContentLoaded
instead is it triggers as soon as the HTML DOM is built but before all images CSS and other external references is loaded.
That has always worked for me - though I use jQuery's ready
event to make it work cross-browser. And it keeps your HTML valid.
I'm seeing malicious Javascript code injected exactly like this, which somehow makes a blank space at the top of a WordPress page. If you click that blank space, you're taken to a site that talks about crypto. The malicious script uses the Javascript atob() function which when then deconverted with base64 and html-escaped causes the crypto page to be loaded.
Just so you know...
本文标签: javascriptAdding the script tag before Doctype DeclarationStack Overflow
版权声明:本文标题:javascript - Adding the script tag before Doctype Declaration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741928481a2405435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论