View Full Version : inserting javascript variables into HTML
smanning
01-08-2003, 03:30 PM
In ASP you can insert an ASP variable directly into your HTML code by setting it off like this <%=variable%>.
Is there a way to do this in Javascript?
Thanks,
Steve
cg9com
01-08-2003, 03:36 PM
var = "thingy";
i dont think you can call a variable from another javascript though, as you can add a variable through server side.
beetle
01-08-2003, 05:41 PM
You'd have to use a full script tag
<script type="text/javascript">document.write(variable);</script>
Or you could use some sort of DOM compatible method for changing text on the page to the value of a variable.
Borgtex
01-08-2003, 05:42 PM
I think that he means
<script>document.write(variable)</script>
cg9com
01-08-2003, 05:49 PM
....the variable can be recognized inside another different script tag?
beetle
01-08-2003, 06:03 PM
Originally posted by cg9com
....the variable can be recognized inside another different script tag? Yes, if it's global, that is.
smanning
01-08-2003, 11:44 PM
This works for inserting the variable into text, but what if I want to insert it into a parameter in an HTML tag, like this:
<input type="hidden" name="whatever" value="(variable)">
It won't let me put a script tag inside another tag, will it?
brothercake
01-08-2003, 11:59 PM
then
document.write('<input type="hidden" name="whatever" value="'+variable+'">');
chrismiceli
01-09-2003, 12:05 AM
all this over
var hi="hi";
a simple line of code
:o
whammy
01-09-2003, 12:56 AM
Just curious, (and here comes my famous question)... what are you really trying to accomplish?
Don't forget "Who, what, when, where, why, and how" - in plain english, not code! ;)
OR...
Give us your code example, and explain exactly what isn't working (that is, if brothercake's example didn't already solve your problem!).
If you're using ASP, there must be a myriad of solutions for what you're attempting. Can we have the whole story? :)
smanning
01-10-2003, 09:06 PM
I'm not asking how to declare a variable. :rolleyes:
The whole story is this: I just started work at a place that has an extremely lame IS department and we have no true web development environment. They won't allow us to set up any server-side scripting support on our intranet because they consider it a security risk. So, I was trying to find a way to make client-side scripting do something I'm used to doing with server-side scripting: that is, intermingle the script with the HTML.
I forget the technical term for this, but in ASP and PHP you can just move back and forth from HTML to script in the same line of code. In ASP you do it like this:
<% myvariable=Steve %>
<div> Now we are in HTML. My name is <%=myvariable%>.
</div>
This would display in the browser:
Now we are in HTML. My name is Steve.
I was looking for a way to do something similar to this type of syntax in Javascript, though I suspect none exists.
Thanks,
Steve
beetle
01-10-2003, 09:09 PM
Myself and Borgtex gave you the most direct way.
In case you are interested, I've a got a few scripts that use javascript to mimic server-side functionalities, such a virtual DBs, date handling, query-strings, etc.
smanning
01-10-2003, 09:10 PM
I just noticed brothercake's suggestion of putting the entire line into a document.write. This does indeed work. I was hoping to avoid having to put long sections of HTML code into document.write all over the place so thought I'd check here to see if there was a way to do this without it.
Thanks!
beetle
01-10-2003, 09:14 PM
How much HTML-code are you trying to insert? Is it mostly just text, or something like entire HTML tables?
You can use the DOM to avoid a bunch of document.writes...
brothercake
01-10-2003, 10:07 PM
There's a specific reason why I suggested the document.write the whole element approach, basically: if the functionality of a web application is dependent on javascript, then using javascript to write the html which controls it is a good idea - that way, you avoid noscript visitors trying to interact with a form they can see but not use.
Even said .. the DOM after-approach is probably better anyway; ergo, document.write the form in javascript quite plain, then manipulate its values through the DOM.
javascriptdummy
11-15-2005, 06:52 PM
Look at the code where it says "width:500px;height:300px"
Ideally, I'd like to replace 500 with widthOf variable, and 300 with heightOf variable. None of the examples seemed to work though. I am newbie to javascriipt.
<!--start of file!-->
<html>
<script language="JavaScript">
var heightOf=0;
var widthOf=0;
function makeheight(...){...}//make height
function makewidth(...){...}//make width
...
</script>
...
<div id="Folder" class="folder"
style="position:absolute; left:170px; top:162px; width:500px;
height:300px; z-index:2; visibility:hidden; overflow:auto">
blablabla
</div>
...
</html>
<!--end!-->
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.