Ellegant inserting js code in dynamically created file

  • Thread starter Thread starter mamin
  • Start date Start date
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?
 
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
 
Thanks, that might to be the only one logic solution. Anyway it looks
better then writing

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

Thanks,
Marcin
 
Back
Top