Setting Text In Javascript

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a web page which uses some javascript. It has a variable which
contains text which I want to retrieve from a file. I have no real knowldege
of javascript so I was looking for a way to get this javascript to read and
text file for the information.

Can I use ASP.Net to set the variable value for the javascript? Or is there
an easier way?
 
Hi Jim,

You didn't mention whether the file is server-side or client-side, but I'll
assume server-side. JavaScript cannot read server-side files, although Ajax
can, which uses JavaScript. However, I don't think that's necessary, unless
the file needs to be read AFTER the page is sent to the client. So, I would
use ASP.Net server-side to read the file and inject the text into the page
(possibly inside the JavaScript).

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Thanks Kevin.

Yes, it is a server side file.

I was not quite sure how to "inject" the text into the page.

So I open up a reader and read all the text into variable XX. Now how do I
set the javascript variable FF to the value which I have in XX? That was the
real point of my confusion, although you did confirm that javascript will not
read server side files.
 
the easiest is to put the text into a hidden field.

<script runat=server>
Page.ClientScript.RegisterHiddenField("fileText",getFileText());
</script>
<script>
alert(document.getElementById('fileText').value);
</script>


-- bruce (sqlwork.com)
 
You can also insert it into a block of JavaScript which is added to the page
using RegisterClientScriptBlock, etc. Or, you can put a LiteralControl into
the hard-coded JavaScript in a Page (if you have hard-coded JavaScript in
the Page_, and set the Text property of the LiteralControl.

The most dynamic but difficult method is to create an HttpHandler to handle
requests for .js files, have the HttpHandler dynamically generate the .js
file, and reference it as an external script in the Page. This is almost
never necessary, and because it is complicated, should be avoided if not
necessary.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Back
Top