First off, there are a several logic/design errors:
- It is
document.readyState
(different capitalization).
- While parsing the
<head>
section, your document will NEVER have document.readyState == "interactive"
because the document doesn't switch to that state until it is done parsing the <body>
, but by definition it is only parsing the <head>
section at this moment.
- If you thought your code would somehow fire when the
readyState
became == "interactive"
, then that's just not how a simple if
statement works. It tests that condition at the moment the code runs and if it's falsey
which it is here, then it will never execute.
If you want to run code when the document is loaded and the DOM is ready to modify and interact with, you can see a summary of your options in this answer.
Your options boil down to this:
Move your javascript to a script tag right before the </body>
tag and remove the if
test you have. At that point in the document parsing, it is safe to interact with any DOM element that was defined before the script.
In modern browsers, add an event listener for the DOMContentLoaded
event and run your code when that event fires.
For more general support including older browsers, get a more complete implementation of a function that will notify you when the browser is safe to interact with. You can see such an implementation that works in pretty much all browsers in use today in this other answer.
Use some sort of javascript framework that already offers you a cross browser way to know when the DOM is ready. jQuery offers $(document).ready()
as do pretty much all other similar frameworks.
This simplest solution to your code would be this:
<html>
<head>
</head>
<body>
<form name="form1" id="form1" method="post">
<input type="submit" id="submit" name="check" onclick="check()">
</form>
<script>
alert(document.getElementById("form1").method);
</script>
</body>
</html>
Working demo: http://jsfiddle.net/jfriend00/Ka4rL/
P.S. I presume you want your <input>
tag inside the form. And, I'd recommend you change to using document.getElementById("form1")
rather than forms[0]
as this is a much more robust way to program because your code doesn't break if someone adds a new form to the page.