Ellegant inserting js code in dynamically created file

M

mamin

Hi,
I need to create html file dynamically from C# code. I need to place
there some javascript code. This js code I'm keeping in fukctions.js
file. Now, I'm generating HTML code, for example:

string htmlContent ="<html><body></body></html>";
......
saveHtmlFile(htmlContent);


and I would like to do something like:
htmlContent+=jsCodeFromFile();before calling saveHtmlFile()

Of course I need to keep this js code in my html file, not in separate
file.
Does anyone know some good solution?
 
S

Superman

Good day,

For starters, I'd highly recommend using the HtmlTextWriter to build
some html dynamically, or at least the StringBuilder class.

Once you get that organized, it's easy to extract the contents of a
file:

[using system.io]

FileStream jsFile = new FileStream(Server.MapPath("functions.js"),
FileMode.Open);

byte[] jsBytes = new byte[jsFile.Length);

//depending on how you're writing to your file, you can either write
some bytes to it orconvert your bytes into a string using this..

string jsFileContents =
System.Text.UTF8Encoding.UTF8.GetString(jsBytes);

//do whatever you need to here...
jsFile.Close();

Hope this helps...

-Brenton
 
M

mamin

Thanks, that might to be the only one logic solution. Anyway it looks
better then writing

jsString+="<script>"
jsString+="......"
jsString+="</script>"

Thanks,
Marcin
 

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

Top