1

As the title suggests, I'm trying to inject a doctype at runtime on a page-by-page basis.

The application uses a single master with a gazillion content pages, so naturally I've tried inserting an asp:contentplaceholder control on the master, and using an asp:content control on the content page. This works in that the doctype element shows up when you view source, but it doesn't work in that the browser (IE8) is still running in Quirks mode for some god-forsaken reason.

Here's the placeholder on the master:

<asp:contentplaceholder id="doctype" runat="server" />

Here's the content panel on the page:

<asp:content contentplaceholderid="doctype" runat="server" >
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
</asp:content>

I'm a bit baffled as to why this isn't working, since the replacement should happen server-side, then the output gets sent to the browser with the doctype already in place...

Why am I doing this?

I'm only doing this because specifying a doctype on the master means I would then need to go and fix the gazillion content pages before I can even begin to work on the assigned task. If I can figure out a way to inject a doctype via certain content pages, then I can effectively fix one page at a time until they are finished.

Which browsers does this affect?

Internet Explorer 8 is our primary target. It is, ironically, the browser that is effectively ignoring the injected doctype. When checking document.doctype at runtime after the DOM is loaded, it's returning null.

Firefox behaves differently. This technique actually works in Firefox, but doesn't really help [me] since all of our users are stuck with Internet Explorer 8.

Cypher
  • 2,608
  • 5
  • 27
  • 41
  • 1
    I could imagine that there might be some additional characters _before_ the DOCTYPE definition. Maybe just some non-printable. I think even blanks and new lines count. – Uwe Keim Jul 27 '12 at 18:02
  • 1
    [Here is a similar SO posting](http://stackoverflow.com/questions/4749011/problem-with-whitespace-before-doctype) that talks about white-space before DOCTYPE. – Uwe Keim Jul 27 '12 at 18:49

3 Answers3

1

Is compatibility mode switched on in IE? i.e. does the browser show the 'jagged page' icon on the right hand side of the URL bar? We had some issues with this being activated by default on some intranet sites for some users. I believe we had to make sure that the intranet site was trusted in their local IE security settings.

Oliver Gray
  • 874
  • 6
  • 17
1

You can try to change the DOCTYPE programatically on your content pages. I have tested this and works even on Internet Explorer.

Remove the DOCTYPE on your MasterPage pagesource first and then try the following code on your Content Page:

Imports System.IO

Public Class WebForm2
    Inherits System.Web.UI.Page

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)    

        Dim sb As New StringBuilder("<!DOCTYPE HTMLP>") 
        Dim textWriter As New HtmlTextWriter(New StringWriter(sb))
        MyBase.Render(textWriter)
        writer.Write(sb.ToString())

    End Sub

End Class
CoderRoller
  • 1,239
  • 3
  • 22
  • 39
0

As it turns out, there was a comment above where the doctype was being rendered. Internet Explorer 8 didn't like this. Once removed, the browser then rendered the page in standards mode.

Cypher
  • 2,608
  • 5
  • 27
  • 41
  • In FF I got " Almost standards mode doctype. Expected “ ” ". I fixed this typing the actual DOCTYPE instead of copy/pasting it in the StringBuilder constructor, checked then on IE8 and the IE standard document mode is enabled with no problems. Try typing the DT you wanna inject, and be sure is the correct DT as well. – CoderRoller Jul 28 '12 at 16:40