When I use the Response.Write()
method in asp.net to output javascript code, such as the alert method, some of the css styles in this page are changed.
For example,when I use the code in asp.net like this:
Response.Write("<script>alert('success!')</script>");
When I run the file, first it popup the alert message, but after the page loads completely, I find that the font-size style is 开发者_运维问答changed and becomes larger, but others don't change.
I know use the Page.ClientScript.RegisterStartupScript()
method that can solve the issue, but I want to know what causes this problem.
That is because you are using Response.Write
where you are not supposed to use it. You are using it outside of the code that is generating the page content, which means that you put the Javascript code outside of the HTML document.
When you have anything before the doctype tag, the doctype is ignored and the page is parsed as tag soup instead of as a valid document, and it is rendered using Quirks Mode instead of Standards Compliance Mode. This affects how the HTML and CSS is interpreted, as the browser tries to emulate an ancient browser version, including all non-standard quirks, and errors. It will also use different default styles for the elements, which is the likely reason for the difference in font size that you see.
Internet Explorer is especially sensetive when Quirks Mode is used. It will include the box model bug to try to be compatible with early versions of the browser.
精彩评论