JavaScript document.write is not working
Solution 1
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>');
Solution 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.
Maria Ines Parnisari
Updated on June 22, 2022Comments
-
Maria Ines Parnisari almost 2 years
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...