Link to home
Start Free TrialLog in
Avatar of kanden
kanden

asked on

ASP.net and Javascript

I have a huge Javascript file .js, I need to add information from a Database to it in certain places. I have tried response.write("file.js")

Is there a way to do this?
Avatar of kollu
kollu

Try this
<script language="javascript" src="file.js" type="text/javascript"></script>
Avatar of kanden

ASKER

Sorry, I am being to vague.

I need to add information inside the Javascript file dynamiclly with asp.net
           Dim reader As StreamReader
            reader = New StreamReader(Page.Server.MapPath("YourFile.js"))

            Dim script As String = reader.ReadToEnd()
            Page.RegisterClientScriptBlock("YourScript", script)
Otherwise... you need to create the javascript on the fly (basically using a StringBuilder). And then register the script... like this:

Dim script As StringBuilder = New StringBuilder(4096)
            Dim CRLF As String = ControlChars.CrLf
            With script
                  .Append("<script lang=JScript> " & CRLF)
                  .Append("function IsValidDateTime(source, args) " & CRLF)
' Lots of javascript removed
                  .Append("</script> ")
            End With

            Page.RegisterClientScriptBlock("YourScript", script.tostring)
Avatar of kanden

ASKER

Ok,


I like option 1 the best. If I get it the reader is reading the file to the end then adding my script to the end of the file. Is there a way to specify a point in the file to start writing and will this parse before the:

<script language="javascript" src="file.js" type="text/javascript"></script>

??
The RegisterScriptClientBlock adds a script block to the output, if it has not already been added. It checs for the existence of a block called "YourScript".

So... you don't need to specify a point to write to, and it parses just fine.
Avatar of kanden

ASKER

The problem is that I need to add lines to certain functions within the file.

am I not getting it?
ASKER CERTIFIED SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CooPzZ
you sound like your trying to do things a bit backwards here..

what and how much are you trying to write to the javascript...

if most of the javascript is generic and used enough times you put as much into the .js file and use what others have said
<script language="javascript" src="file.js" type="text/javascript"></script>

then in the building of the page you use the registerclientscript blocks to add the extra stuff you need..  It's the power of functions that you souldn't really need to add lines to a js file..

Like I said before.. it might help to see an example of what your trying to add.

CooPzZ