Home > Archive > ASP .NET > March 2004 > Dynamically include client-side javascript 2
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Dynamically include client-side javascript 2
|
|
| Andrea Williams 2004-03-31, 12:43 pm |
| 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
On Mon, 29 Mar 2004 10:38:50 -0800, "Andrea Williams"
<andreawil@hotmail.IHATESpam.com> wrote:
>I have a debug component that writes certain info into a DIV so that I can
>see the things I need to debug the production server (It's only enabled for
>my IP address). I also have client-side JavaScript that floats this div on
>top of everything else in the browser and keeps it al the bottom of the
>browser when I scroll it. My Debug component writes the JavaScript needed
>to start the Floating script.
>
>Right now, I have a component that all my pages inherit from that uses this
>Debug class and automatically generates the div and start code when the
>Debug config is enabled. However, I still have to add a JavaScript
<script>
>tag to every page and I would like to be able to do this in the debug class
>so that I don't have to worry about adding the code
><script src="include/FloatDebug.js" language="javascript"></script>
>
>to each page.
>
>Is there a good way to add the js code from my class to the page without
>doing a Response.write for every line? The js file code may change, so I
>don't want to hard-code it into the class.
>
>Also pages may be in a different folders of the application, so simply
>writing the about code in a response.write, won't necessarily work.
>
>Anyone have any ideas?? Is there a way to read the js file to the browser,
>or somehow include it from my class, relative to the class instead of the
>page? Writing it to the browser is the only way I can think of getting
what
>I'm going to need, right now. However, I don't particularly like to expose
>the client side code on the page itself either, but I could live with that
>if it would accomplish my goal.
>
>Thanks in Advance!
>Andrea
>
| |
| Kevin Spencer 2004-03-31, 1:38 pm |
| You can't use any Controls outside of a WebForm. This is just a guess, but I
would expect that the string passed to the function is converted to a
Control prior to inserting it in the Page (all HTML in the Page Template is
converted to Controls at run-time). Is there some special reason you don't
want a form tag in the Page? If so, you could always use the old
Response.Write() method.
--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Andrea Williams" <andreawil@hotmail.IHATESpam.com> wrote in message
news:#hOWf9zFEHA.2876@TK2MSFTNGP09.phx.gbl...
> 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
>
> On Mon, 29 Mar 2004 10:38:50 -0800, "Andrea Williams"
> <andreawil@hotmail.IHATESpam.com> wrote:
>
can[color=darkred]
for[color=darkred]
on[color=darkred]
needed[color=darkred]
this[color=darkred]
> <script>
class[color=darkred]
browser,[color=darkred]
> what
expose[color=darkred]
that[color=darkred]
>
>
| |
| bruce barker 2004-03-31, 1:38 pm |
| its the HtmlForm control that renders client script, so if you do not have
one, it will not render. if you are not using forms, then you can use the
asp:placeholder and the HtmlGeneric control to output script.
HtmlGenericControl myScript = new HtmlGenericControl("script");
myScript.InnerText = "alert('hi')";
myScriptPlaceHolder.Controls.Add(myScript);
-- bruce (sqlwork.com)
"Andrea Williams" <andreawil@hotmail.IHATESpam.com> wrote in message
news:#hOWf9zFEHA.2876@TK2MSFTNGP09.phx.gbl...
> 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
>
> On Mon, 29 Mar 2004 10:38:50 -0800, "Andrea Williams"
> <andreawil@hotmail.IHATESpam.com> wrote:
>
can[color=darkred]
for[color=darkred]
on[color=darkred]
needed[color=darkred]
this[color=darkred]
> <script>
class[color=darkred]
browser,[color=darkred]
> what
expose[color=darkred]
that[color=darkred]
>
>
| |
| Andrea Williams 2004-03-31, 2:39 pm |
| No particular reason to not include a form, just that this particular page
doesn't need one. I have no controls on this particular page.
My alternative is to create some kind of code that will figure out on the
fly where the debugfloat.js file is in relation to the current page and set
the scr in a javascript tag.
All of my pages are inheriting from ASPCommon.cs and I wanted to be able to
load the js script without having to add HTML to each aspx page. This js
file is only needed when my Debug class is enabled, and it's only going to
be for my IP address. I don't want the js file included for anyone else.
This will go on the production server so that I can more easily troubleshoot
problems. Because I don't want it showing up when Debug is disabled, I
wanted to add it dynamically to the page. I'd really like it to be added
inside the HEAD tags, but haven't figured out a way to do that yet.
Any other ideas?
Andrea
"Kevin Spencer" <kevin@takempis.com> wrote in message
news:ega56Z0FEHA.712@tk2msftngp13.phx.gbl...
> You can't use any Controls outside of a WebForm. This is just a guess, but
I
> would expect that the string passed to the function is converted to a
> Control prior to inserting it in the Page (all HTML in the Page Template
is
> converted to Controls at run-time). Is there some special reason you don't
> want a form tag in the Page? If so, you could always use the old
> Response.Write() method.
>
> --
> HTH,
> Kevin Spencer
> .Net Developer
> Microsoft MVP
> Big things are made up
> of lots of little things.
>
> "Andrea Williams" <andreawil@hotmail.IHATESpam.com> wrote in message
> news:#hOWf9zFEHA.2876@TK2MSFTNGP09.phx.gbl...
doesn't[color=darkred]
> design?
the[color=darkred]
> can
> for
div[color=darkred]
> on
> needed
> this
> class
without[color=darkred]
I[color=darkred]
> browser,
the[color=darkred]
> expose
> that
>
>
|
|
|
|
|