3

Sorry if this seems dumb, i'm new to JavaScript.

This is in menu.js:

document.write("<a href="index.html">Home</a>");
document.write("<a href="news.html">News</a>");
document.write("<a href="about.html">About us</a>");

This is in index.html:

<head>
</head>
<body>
    <script type="text/javascript" src="menu.js"></script>
</body>
</html>

When I load index.html, nothing comes up...

2
  • 5
    Learn how to debug JavaScript and you will know what the problem is. Commented Aug 16, 2013 at 14:55
  • Or better yet, use an editor that highlights these errors for you, so that they're caught even before you test it. Commented Sep 16, 2014 at 9:19

2 Answers 2

8

The problem is your quotes, you're using " both to delimit your new elements and to set their href attribute, change your code to:

document.write("<a href='index.html'>Home</a>");
document.write("<a href='news.html'>News</a>");
document.write("<a href='about.html'>About us</a>");

Or:

document.write('<a href="index.html">Home</a>');
document.write('<a href="news.html">News</a>');
document.write('<a href="about.html">About us</a>');

Combining single (') and double (") quotes. You could also escape your internal quotes (document.write("<a href=\"index.html\">Home</a>");

BUT it'd be better to use a single call to document.write(), like this:

document.write('<a href="index.html">Home</a>' 
    + '<a href="news.html">News</a>'
    + '<a href="about.html">About us</a>');
Sign up to request clarification or add additional context in comments.

Comments

2

You're not escaping the quotes in your strings. It should be:

document.write("<a href=\"index.html\">Home</a>");

Otherwise, JavaScript thinks the string ends after href= and the rest of the line does not follow valid JavaScript syntax.

As @Felix mentioned, the JavaScript debugger tools will be extremely helpful in letting you know what's going on.

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.