faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

27 of 40 people (68%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

Can you refer to a javascript variable in an html tag?

Apr 4th, 2008 20:57
ha mo, Iván Rivera, Joseph Greenwood,


Yes, you can, but only inside so-called event handlers, or as part of a
'javascript:' URL. The only limitation is that you should have assigned
the variable before using it in the HTML; either on the HEAD section, or
on a SCRIPT conveniently placed before your JavaScript-calling HTML.
A simple example follows:
Suppose you have this script:
<script language="JavaScript"><!--
  var foo="Don't click here!";
// --></script>
Then, you can use that variable in some event handler:
<a href="#" onMouseOver="alert(foo)">Click me if you dare!</a>
'javascript:' URLs are a way to execute functions from clicks on links,
by means of the href attribute. Since functions may or may not return
values, and the final result of a javascript: URL depends upon the
returned value of the called function, it's usually easier and less
error prone to accomplish the same thing via the onClick event handler,
leaving href pointing to nowhere (a clean way to do that is using the
'#' symbol).
Hope this was useful.
Iván
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com