admin管理员组

文章数量:1430532

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?

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?

Share Improve this question edited Jan 2, 2014 at 20:15 Sotirios Delimanolis 280k62 gold badges715 silver badges738 bronze badges asked Jan 2, 2014 at 20:15 wjhplanowjhplano 5912 gold badges13 silver badges31 bronze badges 6
  • 1 Java is not javascript. javascript is not Java. – Sotirios Delimanolis Commented Jan 2, 2014 at 20:16
  • 1 Check your Javascript console, you're getting syntax errors on the lines that begin with String. – Barmar Commented Jan 2, 2014 at 20:17
  • What do you mean "it doesn't load anything"? – geedubb Commented Jan 2, 2014 at 20:18
  • Why are you leading your accountLink and accountLogo with String, there are no Strings in JavaScript, only Objects. Use var instead. – zero298 Commented Jan 2, 2014 at 20:19
  • when I run it it is as if the write functions aren't executing. as if they weren't there at all. at least that's why I see when I load the web page. – wjhplano Commented Jan 2, 2014 at 20:20
 |  Show 1 more ment

4 Answers 4

Reset to default 2

Use

var accountLink = "#";
var accountLogo = "img/myLogos/someLogo.png";

Declare your string variables with var

  var accountLink = "#";
document.write(accountLink);

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>

You should initialize variable instead of String using var and then try:

var mytext = "Hello again";
document.write(mytext);

Here is working demo

本文标签: htmljavascript documentwrite variablesStack Overflow