A
Andrea Williams
So I'm using a tweaked version of the code below, but when my page doesn't
have a form, the script doesn't seem to be registering. Is that by design?
and if so, is there another way to get a JS file to be registered when the
page doesn't have a form?
Andrea
submitted by Hugo:
You could read the script from disk instead of hard-coding it and
register it as a client script instead of using Response.WriteLine,
which always is a good idea. Because it is often used you could add
the script to cache instead of reading it from disk every time.
if(!Page.IsClientScriptBlockRegistered("myFunctionKey"))
{
FileInfo scriptFile=new FileInfo(MapPath("/scripts/float.js"));
if(scriptFile.Exists)
{
StreamReader reader=new StreamReader(scriptFile.FullName,true);
try
{
string script=string.Format(
"<script language='javascript'><!-- {0} --></script>",
reader.ReadToEnd());
Page.RegisterClientScriptBlock("floatKey",script);
}
finally
{
reader.Close();
}
}
}
/Hugo
have a form, the script doesn't seem to be registering. Is that by design?
and if so, is there another way to get a JS file to be registered when the
page doesn't have a form?
Andrea
submitted by Hugo:
You could read the script from disk instead of hard-coding it and
register it as a client script instead of using Response.WriteLine,
which always is a good idea. Because it is often used you could add
the script to cache instead of reading it from disk every time.
if(!Page.IsClientScriptBlockRegistered("myFunctionKey"))
{
FileInfo scriptFile=new FileInfo(MapPath("/scripts/float.js"));
if(scriptFile.Exists)
{
StreamReader reader=new StreamReader(scriptFile.FullName,true);
try
{
string script=string.Format(
"<script language='javascript'><!-- {0} --></script>",
reader.ReadToEnd());
Page.RegisterClientScriptBlock("floatKey",script);
}
finally
{
reader.Close();
}
}
}
/Hugo