1

I don't know much about this, just having recently started with JavaScript, so simple language would be greatly appreciated.

Whenever I insert JavaScript on a page, it appears in the preview as text on the page and when I update my site it also appears this way. How would I fix this?

Thank you in advance.

1
  • You might be not inserting javascript in <script> </script> tag Commented Sep 1, 2010 at 18:09

3 Answers 3

3

There are two ways to inject JavaScript into a page.

Inline in either the head or body of your html page.

<script type="text/javascript">
    alert('this is inline javascript');
</script>

Or you can link to an external JavaScript file from the head of your html document.

<script type="text/javascript" src="path/to/script.js"></script>

Just remember: if the javascript is an external file, save it with the .js extension.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. This is all very useful. It also makes me realize how much I have to learn. Foo instance, I don't even have a good grasp of how you comment something out in javascript. Is there a really good, easily understandable manual online somewhere? I'm gradually learning flash and I'm trying to make a series of overlays for flash-based audio players.
I would recommend starting off by watching Youtube videos on JavaScript (TheNewBoston has the best series), then I would pick up a book (JavaScript: The Definitive Guide does the best job of explaining advanced concepts).
3

Do you have it within <script type="text/javascript"></script> tags?

1 Comment

Thank you. I feel a little foolish, but I suspect that I might inadvertently have left that out.
0

You need to provide a sample of what you are doing for us to help you.

However, you might check that your script declaration looks something like this:

<script type="text/javascript">
<!-- //html comment hides script from really old browsers

  myVar = resultsFromAMethod();

//-->
</script>

1 Comment

I think it turned out that I hadn't put in the <script type= and </script>, which shows me up pretty clearly as being a real novice. I kind of came across a solution by chance while pasting this line of code for lightbox: <script type="text/javascript" src="lightbox.js"></script> But you make a very good point -- I'll make sure I paste in the code next time so you don't have to guess. Thank you.

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.