Dynamically rendering the content of script

Y

Yama

Hi,

How can I dynamically embed the content of a filename.js into a .aspx page
before rendering it to the client?

Thanks,

~yamazed
 
S

S. Justin Gengo

Yama,

Insert the javascript using Page.RegisterStartupScript. Here's an example:

If Not page.IsStartupScriptRegistered("TextBoxFocus") Then
Dim StringBuilder As New System.Text.StringBuilder

With StringBuilder
.Append("<script language=""javascript"">" & vbCrLf)
.Append(vbTab & "<!--" & vbCrLf)
.Append(vbTab & vbTab & "document.getElementById('" &
webControl.ClientID & "').focus();" & vbCrLf)
.Append(vbTab & "//-->" & vbCrLf)
.Append("</script>" & vbCrLf)
End With

page.RegisterStartupScript("TextBoxFocus", StringBuilder.ToString)
End If

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
Y

Yama

Hi,

My javascript has over 3000 lines of code and this will not be practical.
Anything other way?

Thanks,

Yama
 
B

Bruce Barker

read the file into string, then write it out as explained. you'd be better
off using having the browser fetch it because then it would be cached. 3000
lines is a large payload.

-- bruce (sqlwork.com)
 
K

Ken Varn

See my codeproject article at

http://www.codeproject.com/aspnet/DynamicContentRendering.asp

There is a class in the project called EmbeddedJScript. It basically wraps
a bunch of .NET functions that allow you to embed a JavaScript file as a
resource and then dynamically render it to the page.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
 
Y

Yama

Hi Bruce,

I came up with a better solution:

First I detect the browser and what type of scripting I'll use. I compile
each IE6javascript.js (and all others) into the project as an embedded
resource (select the script's property and change the "Build Action" from
"Content to Embedded Resource) then in my code:

Partial Code:
System.Reflection.Assembly _Assembly = this.GetType().Assembly;

using( System.IO.Stream stream = _Assembly.GetManifestResourceStream(
"myAssemby.MyNamespace.MyScriptFolder.IE6javascript.js" ) )

{

using( System.IO.StreamReader reader = new System.IO.StreamReader(stream) )

{

string _script = reader.ReadToEnd();

Page.RegisterClientScriptBlock( "KeyReferrerNotUSED", _script );

}

Finally perform a cache on server and client side. If the server code
changes then force the change in the client machine.Hint: I use the Observer
design pattern along with the factory method design pattern to achieve all
of this.

Cheers,



~yamazed
 
Y

Yama

Hey Ken,

Not stupid hey! Using the httpModules sounds like an excellent way to handle
caching of the script.

Thanks,

~yamazed
 
Y

Yama

Hey Ken,

I am going to grab some of ya code dude... And I thought I was a smarty
one! LOL I love your handler.

Thanks for sharing (my blogger will soon be up)

~yamazed
 
Y

Yama

Hi Ken,

Everything is working fine for me; however, I cannot figure out a way to
cache the javascript it seems like on every page refresh it reads from the
javascript file. Any ideas how to cache it?

Thanks,

Yama
 
K

Ken Varn

There may be one other way that I have been trying. I have made some
modifications to the code that I published on CodeProject, but I have not
re-submitted it yet. I basically substituted the HttpModule with an
HttpHandler instead. The Handler then can recall the custom data from
server memory without having to re-load it on each postback. The event
handlers are stored in session state, so they do not have to be destroyed on
each postback as it is done on the current implementation. So far I have
only done it on the CustomImage controls, but I don't see why it couldn't be
done on the JavaScript custom handler as well. If you want the source I can
send it to you.


--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top