Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    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

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    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

  • #5
    New Coder
    Join Date
    Feb 2007
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many thanks for all advices and solutions...! I'm trying to digest them all!!



    Best regards


    New comer

  • #6
    New Coder
    Join Date
    Feb 2007
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

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


    Best regards


    New Comer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •