PDA

View Full Version : Inline Variables like ASP.net



magicgavin
09-08-2010, 03:40 PM
Is it possible to reference a javascript variable as you would an asp.net variable.


var test="hello";

<input type="textarea" value="<%=test%>">

Ideally the value of the textarea should now be "hello" and it should update itself if the variable test is changed.

I know it would be easier to assign the textarea a id and dynamically assign the value, but I have a situation where I am loading html code from a text file and putting it into a hta span using innerHTML. I need the loaded html code to populate itself with data using the inline variable method above.

djr33
09-08-2010, 04:29 PM
ASP is processed on the server by a parser that "reads" through the entire page. Javascript, however, is only executed where there are script tags or events. So you will need to use an event.


I wonder if placing this as the value would work....

value="<script type="text/Javascript">
document.write(var);
</script>"

I don't think so, but maybe...

Of course that would cause problems with the page if Javascript was disabled...

jscheuer1
09-08-2010, 05:11 PM
What's:


a hta span

?

magicgavin
09-08-2010, 06:51 PM
What's:



?
The file which I'm trying to do this in is a .hta (Hyper Text Application). I'm trying to load HTML code from a .txt file into a <span></span>. The .txt file contains a template html code that must be able to populate itself using by referring to a javascript variable. I would assign id's to the html code but seeing it is a template it will be used many times and I can't have elements with the same id tag all over the place.

jscheuer1
09-08-2010, 07:53 PM
I see:

http://www.fileinfo.com/extension/hta

According to that, it's a Microsoft only thing. I suppose that if you were running asp.net server side on a Windows server, then it wouldn't matter. You could use PHP (also server side but more commonly available) to make your file an include. A similar syntax could then be used:


<input type="textarea" value="<?php echo $test; ?>">

AJAX may be used in conjunction with javascript to include a file without any special server side code being available. But the syntax would be radically different, and not as convenient.

djr33
09-08-2010, 07:58 PM
PHP has a similar alternate "short tag" syntax for output. I don't like it, but it works (at least on most servers), and it's not actually deprecated, at least not yet (as of even PHP6).

<?=$var?> which is the same as <?php echo $var; ?>