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 notjavascript
.javascript
is notJava
. – 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
andaccountLogo
withString
, there are noString
s in JavaScript, onlyObjects
. Usevar
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
4 Answers
Reset to default 2Use
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
版权声明:本文标题:html - javascript document.write variables - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745491072a2660607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论