Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2007, 02:49 PM   PM User | #1
new_comer
New Coder

 
Join Date: Feb 2007
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
new_comer is an unknown quantity at this point
variables in javascript

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
new_comer is offline   Reply With Quote
Old 02-18-2007, 04:11 PM   PM User | #2
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
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

Code:
<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
ess is offline   Reply With Quote
Old 02-19-2007, 03:28 AM   PM User | #3
new_comer
New Coder

 
Join Date: Feb 2007
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
new_comer is an unknown quantity at this point
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
new_comer is offline   Reply With Quote
Old 02-19-2007, 04:12 AM   PM User | #4
Bobafart
Regular Coder

 
Join Date: Dec 2006
Posts: 417
Thanks: 168
Thanked 1 Time in 1 Post
Bobafart is on a distinguished road
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
Bobafart is offline   Reply With Quote
Old 02-19-2007, 05:25 AM   PM User | #5
new_comer
New Coder

 
Join Date: Feb 2007
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
new_comer is an unknown quantity at this point
Many thanks for all advices and solutions...! I'm trying to digest them all!!



Best regards


New comer
new_comer is offline   Reply With Quote
Old 02-20-2007, 12:12 AM   PM User | #6
new_comer
New Coder

 
Join Date: Feb 2007
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
new_comer is an unknown quantity at this point
Smile

Thanks Ess and Bobafart, the problem has been solved. Many thanks!


Best regards


New Comer
new_comer is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:05 PM.


Advertisement
Log in to turn off these ads.