1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

function and variables - Please help!

Discussion in 'JavaScript' started by publicidadpixelada, Jan 31, 2007.

  1. #1
    Hi, I have the following code in a html document:
    <script type="text/javascript/>
    function [B]something123[/B]() {
    var [B]clickurl[/B] = response.getElementsByTagName('clickurl')[0].firstChild.nodeValue;
    var [B]imgsrc[/B] = response.getElementsByTagName('imgsrc')[0].firstChild.nodeValue;
    document.getElementById("click").innerHTML = clickurl + imgsrc;
    }
    </script>
    Code (markup):
    And it works perfect, but then I try to call in another part of the html document the following code:
    
    <script type="text/javascript/>
    document.write([B]clickurl[/B]);
    document.write([B]imgsrc[/B]);
    </script>
    Code (markup):
    I try to print the variables generated by something123, but it doesn't print anything and I get the following error: imgsrc is not defined - clickurl is not defined

    Any help on how can I print variables generated by a function after the function was executed?

    Thanks in advanced
     
    publicidadpixelada, Jan 31, 2007 IP
  2. Alx

    Alx Member

    Messages:
    94
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #2
    imho you can use only what function returns, not variables defined inside the function
     
    Alx, Feb 1, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    You can define and globalize the variable outside the function using var, and modify it inside the function.

    
    
    <script type="text/javascript">
    
    var clickurl;
    var imgsrc;
     
    function something123() {
    clickurl = response.getElementsByTagName('clickurl')[0].firstChild.nodeValue;
    imgsrc = response.getElementsByTagName('imgsrc')[0].firstChild.nodeValue;
    document.getElementById("click").innerHTML = clickurl + imgsrc;
    }
    </script>
    
    Code (javascript):
    Should work.
     
    nico_swd, Feb 1, 2007 IP
  4. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Yup - Nico_SWD is right - that should work for you
     
    datropics, Feb 1, 2007 IP
  5. publicidadpixelada

    publicidadpixelada Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Worked like a charm, thanks a lot nico_swd!
     
    publicidadpixelada, Feb 6, 2007 IP