...

View Full Version : variables in javascript



new_comer
02-18-2007, 03:49 PM
Greetings,

Hello all, I am new to javascript and have currently encountered problems in using javascript variables. I hope somebody can throw me lights on it. Many thanks!

I want to know if there is any way that I can use variables that were defined and loaded in javascript in html e.g. passing the variable content in href.



Best regards,


New comer

ess
02-18-2007, 05:11 PM
JavaScript is not very strict programming language. Which means, you can declare variables and assign any value to them such as a string, an integer, a float etc.

the following script demonstrates how you can change the value of the href of a link from google.com to codingforums.com


<html>
<head>
<title>Test</title>
<script>
function change( ref ) {
ref.href = "http://codingforums.com";
return true;
} //-- ends
</script>
</head>
<body>
<a href="http://google.com" onclick="return change(this);">google</a>
</body>

</html>

There are other ways to do this, but I am sure that you can use your imagination.

Good Luck
Ess

new_comer
02-19-2007, 04:28 AM
Thanks Ess for your giving me a lot of hints on loading variables. But I do really need some more; I want to know if there is way to change javascript variable for use in html. I hope you can give me some more advices. Many thanks!

------------------------------------------------------------------------
<html>
<head>
<title>Test</title>
<script>
function change(val) {
var n = val.options[val.selectedIndex].value;
return n;
} //-- ends
</script>
</head>
<body>
<form name='aa'>
<select name='k' onChange="return change(this)">
<option value=''>choose</option>
<option value='bb'>test a</option>
</select>
</form>
<br>
<a href="http://test.htm?var=n">google</a>
</body>
</html>

The content of 'n' was lost .....


Best regards


New Comer

Bobafart
02-19-2007, 05:12 AM
new_comer, though ess has posted one possible solution I would stay away from it -- no disrespect to ess of course

ess posted "obtrusive" Javascript: where the markup and the JS mix

what appears to be the new standard in JS web dev is unobtrusive javascript where the JS and HTML don't mix

google "unobtrusive javascript" for more info

new_comer
02-19-2007, 06:25 AM
Many thanks for all advices and solutions...! I'm trying to digest them all!!



Best regards


New comer

new_comer
02-20-2007, 01:12 AM
Thanks Ess and Bobafart, the problem has been solved. Many thanks!


Best regards


New Comer



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum