[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

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?
0
kanden
Asked:
kanden
1 Solution
 
kolluCommented:
Try this
<script language="javascript" src="file.js" type="text/javascript"></script>
0
 
kandenAuthor Commented:
Sorry, I am being to vague.

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

            Dim script As String = reader.ReadToEnd()
            Page.RegisterClientScriptBlock("YourScript", script)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Jeff CertainCommented:
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)
0
 
kandenAuthor Commented:
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>

??
0
 
Jeff CertainCommented:
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.
0
 
kandenAuthor Commented:
The problem is that I need to add lines to certain functions within the file.

am I not getting it?
0
 
Jeff CertainCommented:
In that case, you need to use the second approach -- create the javascript in a stringbuilder and add it to the page using RegisterClientScriptBlock...
0
 
coopzzCommented:
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now