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...
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>');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With