Forum Moderators: open
<script>
var global = null;
document.write("<input type='img' src='n.gi' onerror='global=true;'>");
if (global== null) {
alert("img found");
}
else {
alert("img not found");
}
</script>
I discarded the above code, but if anyone can explain the above strange behaviour, I would certailnly appreciate it.
Thanks!
First, your "global" variable is not a global variable, it's a local variable by the name "global".
Second, it's not always a good idea to use the expression "null" unless you mean exactly that (it is not the same as zero)
Third, the onError event handler clings to a) window, and b) image. When used with "window" it's all lower case, and when used with "image" the E is in upper case. It is not intended for "input".
Forth, onError can be one of these:
a) null
b) the name of a javascript function that returns true
c) a variable that contains null or a property that contains null
- you can not use it for anything that's not a, b, or c.
Here's a rewrite:
<script type="text/javascript">
var global = 0;function doErrorMessage() {
alert("img not found");
global = 1;
}document.write("<img src='n.gi' onError='doErrorMessage()'>");
if (global != 1) {
alert("img found");
}
</script>
<edit>typos</edit>
[edited by: claus at 11:36 pm (utc) on July 4, 2003]
The image onError does not work if you use the <input type='img'>. Only 'the real deal' does: <img> tag.
Also you must call a function in your onError for the code to work.
Furthermore, why document.write HTML when ordinary HTML works as well AND is shorter in this case? AND also non-javascript browsers wil show, or try to show your image.
This certainly does do the job and it's a lot shorter:
<script language="JavaScript">
function gofish() { alert("img not found") }
</script><img src="n.gi" onError="JavaScript: gofish()">
This way there's no need for a 'global' variable or an "img found" alert (obviously the reader will notice it if the image is found).
<John DeVie />
That's why an alert before the if statement works, it gives it time to fire the onerror event before you click ok.
Does that make any sense?
The variable "global" in post #1 is a global variable. It is global because it is not declared or initialized inside a function.
The reason for my mistake is the "var" keyword, and perhaps i was a bit tired as well. I must somehow have convinced myself that the "var global" statement was inside a function when, in fact, it was not.
The thing is, that the "var" is important. If you declare a variable inside a function using the "var" it becomes local. On the other hand, if you declare a variable inside a function without the var, it becomes global. To illustrate, i found this snippet of code somewhere in a js-tutorial [webdevelopersnotes.com]:
var a = 10;disp_a();
function disp_a()
{
var a = 20;
alert("Value of 'a' inside the function " + a);
}alert("Value of 'a' outside the function " + a);
/claus
ricfink, i took your example and pasted it directly into an empty html document (including the buttons), then i opened it in a browser.
The values of "a" were
Inside: 20
Outside: 10
- just as i thought they would be. Meaning: the local variable "a" and the global variable "a" coexist. Even though they have the same name, they have different values.
/claus
<edit>i did not put // before the function call, it worked as it should</edit>
<edit2>even with the // the two vars are still different</edit2>
I misunderstood the fine distinction you were making between referring to the global variable within the function and re-declaring it using var within the function.
So let's leave it at this for any javascript newbies who stumble upon this thread looking for answers:
Variables declared using var (var = x) OUTSIDE a function are global.
If you want to use the same name for a variable within a function (a practice to be avoided), and keep that variable local to the function so it doesn't change the value of the global variable of the same name, then re-declare the variable within the function using var.
Howzat?
I'll subscribe to your summary anytime!
Regarding the original post, devvie and gph also have some good points above.
Oh.. and: Welcome to WebmasterWorld devvie :)
/claus
<edit>added the subscribe part</edit>
It's a timing thing. The browser is doc writing the image then a split second later going through the if statement. The onerror event is firing after the if statement has been read.That's why an alert before the if statement works, it gives it time to fire the onerror event before you click ok.
Does that make any sense?
But this means that you cannot set up any global variable in onload(). Or at least, you cannot rely on it...
BTW, you guys got lost somewhere and forgot about my question:).
HTML:
<input type="image" src="n.gi" onerror="img_error()">
js:
var img_found = true
function img_error() {
img_found = false
}
If you just want to swap the image if there is an error you don't need a global var or a function.
<input type="image" src="a.gif" onerror="this.src='b.gif'">
If none of this is what you're looking for please explain further what you're trying to do.
It's like a delay setting varialbe problem. And I am seeking a reasonable explaination.
Thanks!
<html>
<head>
<title></title>
</head>
<body>
<div id="output"></div>
<script>
var OutPut = document.getElementById('output')function write_error() {
global = true
OutPut.innerHTML += 'onerror event'
}
var global = null
document.write("<input type='image' src='n.gi' onerror='write_error()'>")
OutPut.innerHTML += 'after document.write<br>'
if (global== null) {
//alert("img found");
}
else {
//alert("img not found");
}
OutPut.innerHTML += 'after if statement<br>'
</script>
</body>
</html>