Javascript code working in html but not in JS file.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Javascript code working in html but not in JS file.

Post by tecktalkcm0391 »

I have this:

Code: Select all

function make(){
	
myPara = document.getElementById("paraID");

 
form = document.createElement("form");
form.setAttribute("method","post");
form.setAttribute("action","");
form.setAttribute("name","submit_form");
form.setAttribute("id","submit_form");


first_name = document.createElement("input");
first_name.setAttribute("type","text");
first_name.setAttribute("name","first_name");
first_name.setAttribute("value","chris");
form.appendChild(first_name);
 
myPara.appendChild(form);

} 
With this html:

Code: Select all

<body onload="make();"><div id="paraID"></div>
</body>
How come if I put the code in the header, it works, and if I put it in a seperate JS file I get an error of "Object Expected"?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Sounds like a document error. Try document.form (and maybe change that variable name) and document.first_name.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

When you put the js into an external file, did you remove the onload="make();" attribute?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I'm willing to bet it's an inclusion problem. Your script tag MUST have a closing tag (not a single, closed element) and must specify src="/path/to/your.js"

so make it look like this:

Code: Select all

<script type="text/javascript" src="/path/to/your.js"></script>
The "type" attribute is optional, but I use it anyway.

Make sure the element is empty as well.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

My <script></script> is fine, and everything else is fine, but I ran it in Firefox to get the error, and it says make(); is not defined, but its in the external js file. Any ideas why?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

is your path correct? Recheck your absolute/relative directories and your spelling.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

i got it to now just give me myPara has no properties... any ideas? [it was a */ causin the error before]
Post Reply