Search code examples
javascriptfaviconconditional-comments

Create and inject conditional comment into head with js DOM


I need to inject a favicon into a page of which I have no access to the head section. What I need to append into head is something like this:

<link rel="icon" type="image/png" href="image.png">
<!--[if IE]><link rel="shortcut icon" href="image.ico"/><![endif]-->

I can only inject from a javascript block in body.

The link element is easily made up with document.createElement('link'), but do any of you know how I can create an optional comment?


Solution

  • Actually I just happened to find an answer myself - it wasn't hard at all! :)

    document.createComment('[if IE]><link rel="shortcut icon" href="image.ico"/><![endif]');
    

    It really is just a flat string, and not a DOM structure, but even though head is very closed when I get to do my magic, it appears to work! :-)