0

EDIT:[Honestly this works fine you can read my edit comment below.]

So I am very new to JavaScript. This book I have tells me that I can write the script code in another file that has a .js extension. What it doesn't tell me is what should be in that .js extension.

<html>
<head>
<title>Title of Document</title>

<script src="path/to/file/fileName.js"></script>  
</head>

<body>
The content of
your page goes here. 
</body>
</html>

Lets say I wanted to make an alert message in the java script file. Inside the "fileName.js" would all I write be:

alert("This is an alert box");

and then save it and call it quits? Cause that is what I have so far and nothing doing.

EDIT:

Ok I want to add this in for anyone in trouble like I was. Turns out, this works perfectly. The comments below are a great help for further information. But the thing I did not realize was that on my Mac I needed to start the path to file at /Users. I feel dumb but at least I figured it out. Thanks all for your help.

1
  • is not a valid quotation mark. Commented Jun 5, 2013 at 1:03

4 Answers 4

4

Use " instead of :

<script src="path/to/file/fileName.js"></script>
            ^                        ^
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I already had that. It was some weird formatting thing. still a no go.
1

Generally your js files will have objects and Methods that are called/used from you main page. So you html wiil look like :

<html>
    <head>
        <title>Title of Document</title>

        <script src="path/to/file/fileName.js"></script>  
    </head>

    <body onload="showAlert();">
        The content of
        your page goes here. 
    </body>
</html>

and you js will look like:

function showAlert(){
    alert("This is an alert box");
}

7 Comments

Say it wasnt an alert box and I didn't want it to be on load. Where or how could I use the function then?
When do you want it to run your function?
When ever I want. Is it possible to call it anywhere in the body outside of the <> before the </>
The function can be called from any event, when you click, mouseover, mouseout, on a timer, the possibilities are endless. If you tell me more about what you are doing I can point you in the right direction. ;)
For these purposes say I had some text in the body and I wanted to load that first and then call this function.
|
1

Look into events and listeners. For example, if you want the alert to come up when the page loads, your html file would have:

<body onload="functionName()">

</body>

And you javascript file would have:

function functionName() {
    alert("alert message");
}

Comments

0

Usually you would write your Javascript code as a series of functions that you can call whenever you need. So yes, you can write a single statement the way you did but most times its functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.