Control places JavaScript outside html page

Feb 25, 2010 at 5:28 PM

Hi,

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';
</script>
<!DOCTYPE ....
<html>
....
</html>

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?

Thanks
Feb 26, 2010 at 5:11 PM

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

Example:

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