Will an html image tag execute in the head tag

13,819

Solution 1

It shouldn't. The spec says:

User agents do not generally render elements that appear in the HEAD as content.

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...

Solution 2

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.

Solution 3

Yes. I tested in FireFox and Chrome and it was requested.

Solution 4

Yes, the browser will attempt to load the img resource, but for reasons that aren't immediately obvious.

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.

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.

Since the img is in body, the browser doesn't see it as different from any normal <img> element.

Solution 5

Instead of setting width=1 height=1 I would set style="display:none" to prevent it from rendering and affecting layout.

All browsers will request the image in the header even with 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
Share:
13,819

Related videos on Youtube

Matt Elhotiby
Author by

Matt Elhotiby

Interests: Javascript, React and Rails

Updated on April 30, 2022

Comments

  • Matt Elhotiby
    Matt Elhotiby almost 2 years

    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>
    
    • nico
      nico over 13 years
      Just for the sake of precision... HTML does not execute, it gets interpreted.
  • meawoppl
    meawoppl almost 9 years
    NB: Hacky hacky hacky
  • Aaron McMillin
    Aaron McMillin about 7 years
    But you want to track user with JS off