By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

Dynamically rendering the content of script

P: n/a
Hi,

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

Thanks,

~yamazed
Nov 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

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

Thanks,

~yamazed

Nov 19 '05 #2

P: n/a
Hi,

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

Thanks,

Yama


"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:Oo**************@tk2msftngp13.phx.gbl...
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
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

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

Thanks,

~yamazed


Nov 19 '05 #3

P: n/a
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)


"Yama" <ya**@yamabiz.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

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

Thanks,

Yama


"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:Oo**************@tk2msftngp13.phx.gbl...
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
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

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

Thanks,

~yamazed



Nov 19 '05 #4

P: n/a
See my codeproject article at

http://www.codeproject.com/aspnet/Dy...tRendering.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
-----------------------------------
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

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

Thanks,

~yamazed

Nov 19 '05 #5

P: n/a
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.IE6javascrip t.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



"Bruce Barker" <br******************@safeco.com> wrote in message
news:OA**************@TK2MSFTNGP10.phx.gbl...
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)


"Yama" <ya**@yamabiz.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

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

Thanks,

Yama


"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:Oo**************@tk2msftngp13.phx.gbl...
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
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

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

Thanks,

~yamazed



Nov 19 '05 #6

P: n/a
Hey Ken,

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

Thanks,

~yamazed


"Ken Varn" <nospam> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
See my codeproject article at

http://www.codeproject.com/aspnet/Dy...tRendering.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
-----------------------------------
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

How can I dynamically embed the content of a filename.js into a .aspx

page
before rendering it to the client?

Thanks,

~yamazed


Nov 19 '05 #7

P: n/a
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
"Ken Varn" <nospam> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
See my codeproject article at

http://www.codeproject.com/aspnet/Dy...tRendering.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
-----------------------------------
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

How can I dynamically embed the content of a filename.js into a .aspx

page
before rendering it to the client?

Thanks,

~yamazed


Nov 19 '05 #8

P: n/a
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

"Ken Varn" <nospam> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
See my codeproject article at

http://www.codeproject.com/aspnet/Dy...tRendering.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
-----------------------------------
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

How can I dynamically embed the content of a filename.js into a .aspx

page
before rendering it to the client?

Thanks,

~yamazed


Nov 19 '05 #9

P: n/a
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
-----------------------------------
"Yama" <ya**@yamabiz.com> wrote in message
news:Oz****************@tk2msftngp13.phx.gbl...
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

"Ken Varn" <nospam> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
See my codeproject article at

http://www.codeproject.com/aspnet/Dy...tRendering.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
-----------------------------------
"Yama" <ya**@yamabiz.com> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
Hi,

How can I dynamically embed the content of a filename.js into a .aspx

page
before rendering it to the client?

Thanks,

~yamazed



Nov 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.