...

View Full Version : can't get a simple global variable to work



hogtied
02-21-2003, 10:17 AM
Hello,

I can't understand why this would not work.

<script blah blah>
<!--
var mainForm = document.search;

function blah() {
mainForm.text.value = "blah";
}

-->
</script>

it errors saying null or not an object. ??

the only way i got this to work was to include
"var mainForm = document.search" in every function


thanks
hogtied :confused:

RoyW
02-21-2003, 02:49 PM
Without seeing the rest of the code I will have to assume
1)The code is in the HEAD section of your html.
2)You have a Form named "search"

If this is the case then "document.search" does not exist until the BODY of the document is parsed.

Try
<BODY onLoad="var mainForm = document.search;">

Borgtex
02-21-2003, 03:20 PM
wouldn't it be better...

<body>
<script>var mainForm = document.search</script>

then the function can already run even if there's a big image in the page still downloading?

hogtied
02-23-2003, 07:22 PM
Yes the script is in the head and I do have a form called search.

What your saying makes since in why it does not work.

Thanks

hogtied
02-23-2003, 08:13 PM
neiter one of these worked.

RoyW
02-23-2003, 08:56 PM
Try placing the script after the form

<body>
<form name="search">
</form>
<script>var mainForm = document.search</script>
</body>

hogtied
02-27-2003, 02:12 AM
Thanks roy that worked



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum