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 14 of 14
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting a variable from another function

    Hi

    I'm trying to get a variable named 'html' from one function and use it in another. My JavaScript knowledge is limited, so will be grateful for any help

    Please see code below

    Code:
    
    function createMarker(point, name, label, address, type) {
          var marker = new LabeledMarker(point, {icon: customIcons[type], labelText: label, labelOffset: new GSize(-6, -10)});
          markerGroups[type].push(marker);
    	
    GEvent.addListener(marker, 'click', function() {GEvent.trigger(map,"click",null,point,point); 
    
    var html  = '<div>LatLng:' + name + '</div><a href="javascript:void(0)" onclick="javascript:map.getInfoWindow().maximize()">more info.. </a>'; 
    	  
    	  marker.openMaxContentTabsHtml(html);
          });
          return marker;
        }
    	
    // need to get html variable from the above function into this one below	
    	
    GEvent.addListener(map, 'click', function(ov, latlng, ovll) {
    		  
            if (latlng) {
    			
      var regular = '<div id="sum">LatLng:' + latlng + '</div><a href="javascript:void(0)" onclick="javascript:map.getInfoWindow().maximize()">more info.. </a>';
                var summary = '<div id="sum">Address of' + latlng + '</div><a href="javascript:void(0)" onclick="javascript:map.getInfoWindow().restore()">less info.. </a>';
                var panoDiv = document.createElement('div');
                    panoDiv.width = "400px"; // can be anything, will be auto resized
                    panoDiv.height = "200px";
                var tabs = [new MaxContentTab('address', '<div id="address"></div>'), new MaxContentTab('streetview',  panoDiv)];
                  
    			   map.openMaxContentTabsHtml(latlng, regular, summary, tabs,  { 
                 
    			   maxTitle: "More Information",
                     selectedTab: 'address',// or use index 1,
                       style: {    
                        tabOff: {
                              backgroundColor: '#CCCCFF'
    					
                            }
                          },maximized: document.getElementById('maximized').checked
                        });
                      }
                    });

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    intitialize var html before you run the function in your main code (var html; sould work), this should make it a global variable and you can call it like a normal variable.

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Posts
    115
    Thanks
    2
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by gizmo1650 View Post
    intitialize var html before you run the function in your main code (var html; sould work), this should make it a global variable and you can call it like a normal variable.
    That sounds like something I would do, and it works but...isn't there a better way?

    I've kind of always wondered how it's done properly: how exactly do you go about keeping the global namespace clean? It seems like it's a lot of extra work and lots of passing data around as parameters.

    How do keep global variables to a minimum?

  • #4
    New Coder
    Join Date
    Feb 2010
    Posts
    44
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I only glanced at your code, but to answer your last question, the general idea is to use objects, something like this:

    Code:
    var car = function(model)
    {
      this.model = model;
      this.drive = function(speed){
        //drive at that speed
      }
      this.createMarker = function(...){
        //...
        this.html = "some string";
      }
      this.addListenerToGevent = function(Gevent){
        Gevent.addListener(..., this.html){...};
      }
      this.getHTML = function(){ return this.html; };
    }
    
    var myPorsche= new car("Porsche");
    myPorsche.drive(200);
    
    Gevent.addListener(...){
      ...
      myPorsche.getHTML();
    }
    So as you can see I showed two ways to use the html var in the addListener function... either because the car object is actually responsible for both createMarker as well as creating the listener, then it can just use the shared variable. Or if the car object is not responsible for both, but is just responsible for the createMarker, then it can store the variable and return it for anyone to use. Thus the instantiation of the car object is what is either global or known where it is kept (say in a global list of cars, or even attached as another variable to another object or to an element like a Div) or is passed around.

    So basically you need to use Object Oriented programming in JS. You create classes by defining functions, which is the constructor, and in the constructor you can define functions and members for that class. Then you instantiate that class and use it in other objects or pass it around or whatever. When you design it all using Object Oriented programming then you have all your relationships and objects know of each other (contain pointers to other objects) and you really limit the number of global vars/objects.

    It's all in the design.

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks all

    My next question is could the global html variable still be assigned to the local 'name' variable as in the line:
    Code:
     
    var html  = '<div>LatLng:' + name + '</div><a href="javascript:void(0)" onclick="javascript:map.getInfoWindow().maximize()">more info.. </a>';
    Thanks again

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    No. When you are dealing with variables, local or global, you should use the var token only the first time you create/define them.
    Code:
    var html;
    function myFunction(){
    var name='foo';
    name+='fee';
    html='<div>LatLng:' + name + '</div><a href="#" onclick="map.getInfoWindow().maximize();return false">more info.. </a>';
    }
    Note also the blue up-to-date syntax.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Thanks

    Is it Ok to assign the html variable to the 'name' variable in this way, even though 'name' is not defined locally, but is a parameter of a function.

    Thanks

    Lee

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by leegodden View Post
    Hey Thanks

    Is it Ok to assign the html variable to the 'name' variable in this way, even though 'name' is not defined locally, but is a parameter of a function.

    Thanks

    Lee
    A parameter works like a local already defined variable.
    Code:
    var html;
    function myFunction(name){
    name+='fee';// optional
    html='<div>LatLng:' + name + '</div><a href="#" onclick="map.getInfoWindow().maximize();return false">more info.. </a>';
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your help. And so finally, can I now pass the assigned html global variable to other functions?

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by leegodden View Post
    can I now pass the assigned html global variable to other functions?
    That is nonsense. A Global primitive is to be seen from inside all the functions, thus there is no need to pass it from a function to another. It is enough to change its value from inside a function, if you need that.
    Code:
    var x=5;
    function changeX(){
    x=10; // from now once - if the function was run,  the Global variable x will have the value 10 till a new change
    }
    function alertX(){
    alert(x)
    }
    If you run the function changeX() before calling alertX(), the returned value will be changed from 5 to 10


    In JavaScript a Global is not a Constant.
    Last edited by Kor; 06-14-2010 at 03:20 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, excuse my ingnorance. I just wanted to confirm that if i assigned a global variable to a local variable belonging to one function, I could then use that global variable in another function, even though the variable it's assigned to isn't global?

    Thanks

    Lee

  • #12
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by leegodden View Post
    Sorry, excuse my ingnorance. I just wanted to confirm that if i assigned a global variable to a local variable belonging to one function, I could then use that global variable in another function, even though the variable it's assigned to isn't global?

    Thanks

    Lee
    post an example of what you mean.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #13
    New Coder
    Join Date
    Jul 2009
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var html;
    function a(home)
    {
       html = home;
    }
    
    function b()
    {
      var x  = '<div>LatLng:' + html + '</div><a href="javascript:void(0)" onclick="javascript:map.getInfoWindow().maximize()">more info.. </a>'; 
    }
    Will the global var 'html' still be assigned to local var ' home'?

    Thanks

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,917
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    It is very unwise to give he same name to a Javascript global variable and a local variable.

    Code:
    <script type = "text/javascript">
    
    var html = "X";  // global
    function a(home){
    var html = home;  // local
    alert (html);
    }
    alert (html);
    a("Y");
    alert (html);
    
    </script>
    BTW, You should also avoid giving names or id's to your variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value' or 'text' or 'checked' or 'go' or 'submit' or 'replace' or 'button' or 'radio' or 'parseInt'.
    Last edited by Philip M; 06-15-2010 at 08:32 AM.


  •  

    Posting Permissions

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