Trouble calling a js file from an html file

J

Jake j

Yes, this is a simple and straightforward thing to do as long as the .js
file doesn't use a function. When I try a script with a function in a
js file I can't get it to work (though it works fine within the html
file). I'm guessing that there's some syntax or convention I'm not
doing right.

What I most need is an example. Can someone write a js file with an
alert command, for example, to keep it simple. Perhaps something like:

var name = prompt('What is your name?')
document.write('Hello ' + name)

Then, call this js file from an html file. After you test it and make
sure it works, please send me the two parts.

I've looked but can't find any working examples or discussions of this
particular issue.

~ J
 
L

Lee

Jake j said:
What I most need is an example. Can someone write a js file with an
alert command, for example, to keep it simple.

You don't call a file, you include it.

Here's a working example:
<html>
<body>
<script type="text/javascript"
src="http://www.azphx.com/dhtml/tmp/helloAlert.js"></script>
</body>
</html>

The file helloAlert.js contains a single line:
alert("hello, world!");

It might be more helpful if you post a simple example that
*doesn't* work, for you.
 
A

ASM

Jake said:
What I most need is an example. Can someone write a js file with an
alert command, for example, to keep it simple. Perhaps something like:

file test.html
<html>
<script type="text/javascript" src="test.js"></script>
<a href="#" onclick="alert(foo);">test external js</a>
</html>

file test.js
foo = 'Hello Jake';


other

file test_js.html
<html>
<script type="text/javascript" src="test_js.js"></script>
<a href="#" onclick="result();">test external js</a>
<form>
Result = <input type=text name=result>
</form>
</html>

file test_js.js
function result() {
document.forms[0].result.value=prompt('What is your name?');
}
 
R

Razzbar

ASM wrote:
....
file test_js.js
function result() {
document.forms[0].result.value=prompt('What is your name?');
}

You can't use a javascript function as a form input value. You are
mixing javascript and html. Javascript has to be inside <script> tags.
 
R

Randy Webb

Razzbar said:
ASM wrote:
....
file test_js.js
function result() {
document.forms[0].result.value=prompt('What is your name?');
}


You can't use a javascript function as a form input value. You are
mixing javascript and html. Javascript has to be inside <script> tags.

What exactly is it that you are missing in there? And, did you test that
code as posted? Or, did you miss the part where it said "file
test_js.js" which means it's an external file and you do *not* put
script tags in external files.
 
R

RobG

Jake said:
Yes, this is a simple and straightforward thing to do as long as the .js
file doesn't use a function. When I try a script with a function in a
js file I can't get it to work (though it works fine within the html
file). I'm guessing that there's some syntax or convention I'm not
doing right.

Hey Jake,

I posted a reply to your old thread on 'Calling a .js script for sliding
a text banner'. Figured I'd give you a pointer to down there...


or

<URL:http://groups-beta.google.com/group...g+a+text+banner&rnum=1&hl=en#f1b553008663d22d>
 
A

ASM

Razzbar said:
ASM wrote:
...
file test_js.js
function result() {
document.forms[0].result.value=prompt('What is your name?');
}


You can't use

yes I can -> that's work
a javascript function as a form input value. You are
mixing javascript and html. Javascript has to be inside <script> tags.

Javascript is default browser's script language
 
A

ASM

Razzbar said:
ASM wrote:
...
file test_js.js
function result() {
document.forms[0].result.value=prompt('What is your name?');
}


You can't use a javascript function as a form input value. You are
mixing javascript and html. Javascript has to be inside <script> tags.

demos on line :
http://perso.wanadoo.fr/stephane.moriaux/truc/external_js/test.html
http://perso.wanadoo.fr/stephane.moriaux/truc/external_js/test_js.html
download :
http://perso.wanadoo.fr/stephane.moriaux/truc/external_js.zip
 
J

Jake j

To everyone who sent me examples: they work. I started a new thread
with the code that I'm actually trying to implement.

Please have a look at subject: "Help moving a script from the html to a
js file as an include"

Thanks!!

~ J
 
R

Razzbar

Randy said:
What exactly is it that you are missing in there? And, did you test that
code as posted? Or, did you miss the part where it said "file
test_js.js" which means it's an external file and you do *not* put
script tags in external files.

If you're seriously interested in what I missed, it was that the code I
underlined was in a block of JS code. Very, very obvious if I had read
the entire line.

But just to argue: Correct, you don't put script tags in external
files, but you do put external files in script tags. So there.
 
A

ASM

Razzbar said:
But just to argue: Correct, you don't put script tags in external
files, but you do put external files in script tags. So there.

and even more
to call an external from an internal script ;-)

<script type="text/javascript">

document.write('<script type="text/javascript" src="extern.js">'+
'<\/script>');

</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top