Is it possible to define a global variable in a JavaScript function?
I want use the trailimage
variable (declared in the makeObj
function) in other functions.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
function makeObj(address) {
**var trailimage = [address, 50, 50];**
document.write('<img id="trailimageid" src="' + trailimage[0] + '" border="0" style=" position: absolute; visibility:visible; left: 0px; top: 0px; width: ' + trailimage[1] + 'px; height: ' + trailimage[2] + 'px">');
obj_selected = 1;
}
function truebody() {
return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}
function hidetrail() {
var x = document.getElementById("trailimageid").style;
x.visibility = "hidden";
document.onmousemove = "";
}
function followmouse(e) {
var xcoord = offsetfrommouse[0];
var ycoord = offsetfrommouse[1];
var x = document.getElementById("trailimageid").style;
if (typeof e != "undefined") {
xcoord += e.pageX;
ycoord += e.pageY;
}
else if (typeof window.event != "undefined") {
xcoord += truebody().scrollLeft + event.clientX;
ycoord += truebody().scrollTop + event.clientY;
}
var docwidth = 1395;
var docheight = 676;
if (xcoord + trailimage[1] + 3 > docwidth || ycoord + trailimage[2] > docheight) {
x.display = "none";
alert("inja");
}
else
x.display = "";
x.left = xcoord + "px";
x.top = ycoord + "px";
}
if (obj_selected = 1) {
alert("obj_selected = true");
document.onmousemove = followmouse;
if (displayduration > 0)
setTimeout("hidetrail()", displayduration * 1000);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<img alt="" id="house" src="Pictures/sides/right.gif" style="z-index: 1; left: 372px;
top: 219px; position: absolute; height: 138px; width: 120px" onclick="javascript:makeObj('Pictures/sides/sides-not-clicked.gif');" />
</form>
</body>
</html>
Just declare it outside the functions, and assign values inside the functions. Something like:
Or simply removing "var" from your variable name inside function also makes it global, but it is better to declare it outside once for cleaner code. This will also work:
I hope this example explains more: http://jsfiddle.net/qCrGE/
Yes, as the others have said, you can use
var
at global scope (outside of all functions) to declare a global variable:Alternately, you can assign to a property on
window
:...because in browsers,
all global variablesglobal variables declared withvar
are properties of thewindow
object. (In the latest specification, ECMAScript 2015, the newlet
,const
, andclass
statements at global scope create globals that aren't properties of the global object; a new concept in ES2015.)(There's also the horror of implicit globals, but don't do it on purpose and do your best to avoid doing it by accident, perhaps by using ES5's
"use strict"
.)All that said: I'd avoid global variables if you possibly can (and you almost certainly can). As I mentioned, they end up being properties of
window
, andwindow
is already plenty crowded enough what with all elements with anid
(and many with just aname
) being dumped in it (and regardless that upcoming specification, IE dumps just about anything with aname
on there).Instead, wrap your code in a scoping function and use variables local to that scoping function, and make your other functions closures within it:
No, you can't. Just declare the variable outside the function. You don't have to declare it at the same time as you assign the value:
Just declare
outside. Then
Hope this helps.
Here is another easy method to make the variable available in other functions without having to use global variables: