Jump to content

Writing Function Variables in HTML


Howdy_McGee

Recommended Posts

I can make a script in the Body and put the script there:

<body>  <script type="text/javascript">	  var greeting = "hello";	  document.write(greeting);  </script></body>

but what I want to do is put a javascript function in a separate file then get the variables in the function and print them out in certain places in my html file.I tried:

document.write(Greeting(greetingVariable));

and that didn't work :/I tried

document.write(greetingVariable);

but that didn't work.I can't seem to get the variable form the separate file let along each individual function, any idea on what I should do?

Link to comment
Share on other sites

in external js file you can haveshowgreeting.js

// JavaScript Documentfunction showgreeting(){var greeting = "hello";document.write(greeting);}var greeting2 = "hello";

with html showgreeting.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript" src="showgreeting.js"></script><script type="text/javascript">/*<![CDATA[*//*---->*//*--*//*]]>*/</script><style type="text/css"></style></head><body><script type="text/javascript">/*<![CDATA[*//*---->*/showgreeting();document.write('<br />'+greeting2);/*--*//*]]>*/</script></body></html>

both should work

Link to comment
Share on other sites

could you show us the whole code? The HTML and then the included .js file?It should be sufficient enough to include the .js file with a simple variable declartion (in the head section of your document), but if you are declaring the variable name as greeting, then you should reference it as greeting, which doesn't appear to be what you are doing in these instances. (but you where in the first case).i.e. good

document.write(greeting);

i.e. bad

document.write(Greeting(greetingVariable));

and

document.write(greetingVariable);

edit: dsonesuk posted while I was typing. He seems to be getting you headed in the right direction

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...