473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Trouble calling a js file from an html file

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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #1
11 5551
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.

Jul 23 '05 #2
ASM
Jake j wrote:
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?');
}

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #3


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.

Jul 23 '05 #4
Razzbar wrote:

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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #5
Jake j wrote:
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...

news://inetbws1.citec.qld.gov.au:119/wQ******************@news.optus.net.au

or

<URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/ec67f1a7aef03d1/f1b553008663d22d?q=Calling+a+.js+script+for+slidin g+a+text+banner&rnum=1&hl=en#f1b553008663d22d>
--
Rob
Jul 23 '05 #6
ASM
Razzbar wrote:

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
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #7
ASM
Razzbar wrote:

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.mor...l_js/test.html
http://perso.wanadoo.fr/stephane.mor...s/test_js.html
download :
http://perso.wanadoo.fr/stephane.mor...xternal_js.zip
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #8


~ J

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #9
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

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #10


Randy Webb wrote:
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.

Jul 23 '05 #11
ASM
Razzbar wrote:

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>

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: Freddie | last post by:
Hi, I've been mangling python-irclib into an asyncore class, so it fits in nicely with the rest of my app. I ran into a problem with asyncore.dispatcher_with_send (Python 2.3.4), though. Not...
10
by: gregory_may | last post by:
I have an application I created called "JpegViewer.exe". It simply loads a Jpeg file and displays in on the screen. It works great, in my lab. When I am using it at a customer site, things...
8
by: Jakej | last post by:
I've been using a javascript in an html file for a banner slider, and it works as desired. But I'd like to use it on more than one page and it would be great if I could transfer the code to a .js...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
5
by: mlg1906 | last post by:
I'm developing an intranet site in ASP.NET 2.0 but I can't seem to connect to the DB from within my code. I've created a .vb class that houses a private Connection() that other functions within the...
2
by: bellerophon | last post by:
I am having a serious trouble with my pointers in a programm that uses dll and was hoping that somebody in this group could help me: I export my DLL classes with declspec(dllexport) and link...
0
grassh0pp3r
by: grassh0pp3r | last post by:
Hello, I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.