Hi guys,
Here's the code I'm to refer to:
[HTML]
<html>
<body>
....
<form method="get" action="http://www.google.com/search" name="google" />
<input type="text" name="q" size="25" maxlength="255" value="- search this site -" onclick="ClearIfAppropriate();" class="textbox" />
<a onclick="submit();" href="#">Search</a>
</form>
...
</html>[/HTML]
-
<script type="text/javascript" language="JavaScript" >
-
<!--
-
var LabelText = "- search this site -";
-
-
if(document.google.q.value.length == 0) {
-
document.google.q.value = LabelText;
-
}
-
-
function ClearIfAppropriate() {
-
if(document.google.q.value == LabelText) {
-
document.google.q.value = "";
-
}
-
}
-
//-->
-
</script>
Now for the problem - it's related to the onclick="ClearIfAppropriate();" function. Firefox will execute this function just fine if I embed the js in the html file after the function call; however it fails if I try to do it either of these ways:
* if I embed the code BEFORE the function call, e.g. in the <head> zone where it would normally belong, the code runs properly but FF generates a javascript error 'document.google has no properties' and directs me to the line if(document.google.q.value.length == 0) of the js code.
* if I put the code in a separate file with <script src="...> anywhere on the page, be it in the header zone or after the function call where I previously embedded the code, the function fails when clicking on the text box (which calls the function) and I get a js error 'ClearIfAppropriate is not defined' which directs me to the first line of the html code <!DOCTYPE ....
Now I've linked to code before and had no problems, in fact <a onclick="submit();" href="#"> refers to a function in a linked file
-
function submit() {
-
document.google.submit();
-
}
and this works fine. Clearly one solution is to embed the code after the function call but this is messy and I don't think it should be necessary. It looks to me as though the issue is related to the code being executed before the external function has been imported or something; though why the same works for the other function is beyond me.
Would appreciate any help you can offer.
Thanks!
Greg