Reputation: 591
I'm writing a simple javascript with several document.write statements...
String accountLink = "#";
String accountLogo = "img/myLogos/someLogo.png";
function drawLogos(){
document.write('<li><a href="');
document.write("#"); // link to account page
document.write('"><img src="');
document.write('img/myLogos/someLogoF.png'); // reference to logo image
document.write('" alt="" /></a></li>');
}
Which works just fine. but when I use a variable like this...
document.write(accountLink);
it doesn't load anything. I've tried getting rid of the quotes but nothing different happens. Is there some small syntax error I'm not picking up on?
Upvotes: 1
Views: 4991
Reputation: 28538
You should initialize variable instead of String
using var
and then try:
var mytext = "Hello again";
document.write(mytext);
Upvotes: 1
Reputation: 812
use var
instead of String, Javascript is a weak typing language
<script>
var accountLink = "#";
var accountLogo = "img/myLogos/someLogo.png";
function drawLogos(){
document.write('<li><a href="');
document.write(accountLink); // link to account page
document.write('"><img src="');
document.write(accountLogo); // reference to logo image
document.write('" alt="" /></a></li>');
}
</script>
Upvotes: 1
Reputation: 6521
Declare your string variables with var
var accountLink = "#";
document.write(accountLink);
Upvotes: 1
Reputation: 416
Use
var accountLink = "#";
var accountLogo = "img/myLogos/someLogo.png";
Upvotes: 2