...

View Full Version : Getting a variable from another function



leegodden
06-13-2010, 11:22 PM
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





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
});
}
});

gizmo1650
06-14-2010, 04:11 AM
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.

Dean440
06-14-2010, 05:49 AM
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?

Skychan
06-14-2010, 06:39 AM
I only glanced at your code, but to answer your last question, the general idea is to use objects, something like this:



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.

leegodden
06-14-2010, 10:03 AM
Thanks all

My next question is could the global html variable still be assigned to the local 'name' variable as in the line:


var html = '<div>LatLng:' + name + '</div><a href="javascript:void(0)" onclick="javascript:map.getInfoWindow().maximize()">more info.. </a>';


Thanks again

Kor
06-14-2010, 12:57 PM
No. When you are dealing with variables, local or global, you should use the var token only the first time you create/define them.


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.

leegodden
06-14-2010, 01:09 PM
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

Kor
06-14-2010, 01:40 PM
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.


var html;
function myFunction(name){
name+='fee';// optional
html='<div>LatLng:' + name + '</div><a href="#" onclick="map.getInfoWindow().maximize();return false">more info.. </a>';
}

leegodden
06-14-2010, 02:51 PM
Thanks for your help. And so finally, can I now pass the assigned html global variable to other functions?

Kor
06-14-2010, 03:15 PM
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.


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.

leegodden
06-14-2010, 03:35 PM
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

Kor
06-14-2010, 03:51 PM
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.

leegodden
06-14-2010, 04:01 PM
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

Philip M
06-15-2010, 08:22 AM
It is very unwise to give he same name to a Javascript global variable and a local variable.


<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'.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum