Control places JavaScript outside html page

Feb 25, 2010 at 4:28 PM


Whenever you create the javascript Object of the picture and its dimensions it is published to the top of the page outside the HTML and DOCTYPE defintion, this causes problems, firstly it forces IE into Quirksmode.

<script language="javascript">
var path47dcc303be3c412b8f3b47a55f53109a = '_layouts/images/nopic.png';
var width47dcc303be3c412b8f3b47a55f53109a = '135px';
var height47dcc303be3c412b8f3b47a55f53109a = '135px';
<!DOCTYPE ....

Is there anyway of getting the control to publish the JavaScript object within the html head as this will prevent the issues with the DOCTYPE and IE being forced into Quirksmode?

Feb 26, 2010 at 4:11 PM

I found the solution to the problem above, instead of using "Page.Response.Write" use the clientScript Manager class


String cscriptName = "msPictureVars"; // Give the script a unique name
Type cstype = this.GetType(); // Define the type
ClientScriptManager cs = Page.ClientScript; //Instantiate ClientScriptManager class


if (!cs.IsClientScriptBlockRegistered(cstype, cscriptName))
                    String pictureJSObj = String.Format(@" var path{0} = '_layouts/images/nopic.png'; var width{0} = '128px'; var height{0} = '128px';", Field.Id.ToString("N"));
                    cs.RegisterClientScriptBlock(cstype, cscriptName, pictureJSObj, true);


Related threads:-
script is coming outside the html in the rendered html page
How to: Add Client Script Dynamically to ASP.NET Web Pages