javascript html图像标签会在head标签中执行吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3469587/
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
Will an html image tag execute in the head tag
提问by Matt Elhotiby
So a lead tracking company needs a link to fire and it was in the head tag...will it fire.
因此,潜在客户跟踪公司需要一个链接来触发,并且它位于头部标签中……它会触发吗?
<head>
<img width=1 height=1 src='http://track.searchignite.com/si/CM/Tracking/TransactionTracking.aspx?siclientid=4426&DetailDescription=935626&transactionamount=1&SICustTransType=19172&jscript=0&x10=goog&x9=1&x8=935626&x7=777+665-9999&x6=jones&x5=matt&x4=&x3=Camarillo&x2=Oxnard%2C+CA+Metro+Area&x1=Hidden+Springs&n1=Austin--Bedroom--austin_1_bedroom_apartments-P'>
</head>
回答by Larry K
It shouldn't. The spec says:
它不应该。规范说:
User agents do not generally render elements that appear in the HEAD as content.
用户代理通常不会将出现在 HEAD 中的元素呈现为内容。
See the spec
查看规格
But browsers can do anything they want to.
但是浏览器可以做任何他们想做的事情。
Added:
添加:
It's a loose area in the spec. Eg a browser could:
这是规范中的一个松散区域。例如浏览器可以:
- Render the element
- Ignore the element
- Load the image from its server but not render it
- Or something else
- 渲染元素
- 忽略元素
- 从其服务器加载图像但不渲染它
- 或者是其他东西
... and the browser would be compliant.
...并且浏览器将符合要求。
So see what your favorite browsers do and then use the info as you wish...
所以看看你最喜欢的浏览器做什么,然后按照你的意愿使用信息......
回答by Adam
Yes. I tested in FireFox and Chrome and it was requested.
是的。我在 FireFox 和 Chrome 中进行了测试,并被要求使用。
回答by Guffa
That would depend on the browser. The code is incorrect, and there is no standard for how to handle incorrect code, so it's up to each browser to try to make any sense of it.
这将取决于浏览器。代码不正确,如何处理不正确的代码也没有标准,所以每个浏览器都试图理解它。
I think that most browsers would change to a tag soup mode, and show whatever they can, but some browsers might ignore the image instead.
我认为大多数浏览器会更改为标签汤模式,并显示它们所能显示的任何内容,但有些浏览器可能会忽略图像。
There is also the matter what the incorrect code does to the rest of the page. If the browser changes how the code is parsed, it may have negative effects on the code that is correct.
还有一个问题是不正确的代码对页面的其余部分做了什么。如果浏览器更改了代码的解析方式,则可能会对正确的代码产生负面影响。
回答by Alohci
Yes, the browser will attempt to load the img resource, but for reasons that aren't immediately obvious.
是的,浏览器会尝试加载 img 资源,但原因不是很明显。
Assuming this is served as text/html, when the browser's parser sees the <img>element it will believe that it is encountering displayable content and simply infer the </head>and <body>tags (remember, these are optional in HTML), so the <img>is not actually in the head element, but in the body element.
假设这是作为 text/html 提供的,当浏览器的解析器看到该<img>元素时,它会认为它遇到了可显示的内容并简单地推断出</head>and<body>标签(请记住,这些在 HTML 中是可选的),因此<img>实际上不在 head 元素中, 但在 body 元素中。
If you use firebug, or another means of inspecting the DOM such as http://software.hixie.ch/utilities/js/live-dom-viewer/, you can see for yourself that this is the case.
如果您使用 firebug 或其他检查 DOM 的方法,例如http://software.hixie.ch/utilities/js/live-dom-viewer/,您可以亲眼看到情况就是如此。
Since the img is in body, the browser doesn't see it as different from any normal <img>element.
由于 img 在 body 中,浏览器不会将其视为与任何普通<img>元素不同。
回答by Oscar
Instead of setting width=1 height=1I would set style="display:none"to prevent it from rendering and affecting layout.
而不是设置width=1 height=1我会设置style="display:none"以防止它呈现和影响布局。
All browsers will request the image in the header even with display:none.
所有浏览器都会请求标题中的图像,即使是display:none.
I have checked it on:
我已经检查过:
- MS Edge: 13
- IE: 8, 9, 10 and 11
- Safari: 9 and 6
- FF: 25, 47 and 49
- Chromium: 51
- 微软边缘:13
- 即:8、9、10 和 11
- 野生动物园:9 和 6
- FF:25、47 和 49
- 铬:51
回答by Tim S. Van Haren
Web browsers will indeed load the image. I suspect that the placement of it in the headof the document is because the srcattribute doesn't load an actual image, so it would appear as a broken image in the web page itself. Since it's in the head, it will be called but not rendered on the page.
Web 浏览器确实会加载图像。我怀疑它在head文档中的放置是因为该src属性没有加载实际图像,所以它会在网页本身中显示为损坏的图像。由于它在 中head,它将被调用但不会在页面上呈现。
In effect, that acts like an asynchronous script call, because the browser will perform a GET request for that image's src attribute's URL while loading the page.
实际上,这就像一个异步脚本调用,因为浏览器将在加载页面时对该图像的 src 属性的 URL 执行 GET 请求。
回答by casablanca
Most browsers don't bother about the rules, so it will work. But if you want to be standards compliant, you shouldn't do this. (At least in XHTML, it is invalid to place an <img>tag within <head>.)
大多数浏览器不关心规则,所以它会起作用。但是,如果您想符合标准,则不应该这样做。(至少在 XHTML 中,<img>在<head>. 中放置标签是无效的。)
回答by Dutchie432
Technically yes.. but its much better to just put it in an HTML / JS call..
从技术上讲是的..但是把它放在一个 HTML/JS 调用中要好得多..
HTML
HTML
<body onload="triggerLink('4426');">
JAVASCRIPT
爪哇脚本
function triggerLink(var1){
var receiveReq = getXmlHttpRequestObjectShipping();
var url= 'http://track.searchignite.com/si/CM/Tracking/TransactionTracking.aspx?siclientid=' + var1;
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
receiveReq.open("POST", url, true);
receiveReq.onreadystatechange = handleLink;
receiveReq.send(null);
}
}
function handleLink(){
//Do This when request finished
}
You can add variables to the "tiggerLink" function as well to pass the rest of your URL parameters.
您也可以向“tiggerLink”函数添加变量以传递其余的 URL 参数。
回答by sergio
Most modern browsers will move the head and body up to leave any visible content inside the body. (You can see that in the inspector). It seems so reliable that it will be at least loaded, that Google uses it as a pixel tracking system inside a noscript tag, all inside the head (where they recommend to place the whole code, along with the script) in case javascript is not supported.
大多数现代浏览器会将头部和身体向上移动,以在身体内部留下任何可见的内容。(您可以在检查器中看到)。它看起来非常可靠,至少会被加载,谷歌将其用作 noscript 标签内的像素跟踪系统,全部位于头部内(他们建议将整个代码与脚本一起放置),以防 javascript 不是支持的。
回答by Ray
I suggest using a <script>tag instead of an <img>tag. Then there is no question whether a browser will request the url. Just be sure it doesn't return anything that could cause a script error.
我建议使用<script>标签而不是<img>标签。那么浏览器是否会请求该 url 是毫无疑问的。只要确保它不会返回任何可能导致脚本错误的东西。

