/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] accessing javascript variables in html

i have an html page and an external javascript page.

in the head section of my html i define two variables that are equal to the return value of functions in my .js file.

but when i try and access these variables from the head of my html in the body, I get a “variableName” is not defined error. what am i doing wrong?

thanks for the help

[code]
<head>
<script src=”datetime.js” type=”text/javascript”/>
<script type=”terxt/javascript”>
var timeStr = datetime.showDateTime();
var imgNum = datetime.getImage();
</script>
</head>
<body>
<script type=”text/javascript”>
document.write(“<img id=’image’ src=’image” + imgNum + “.jpg’ alt=” />”);
</script>
</body>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ominubauthorApr 07.2011 — sorry for the double post but i couldnt find an edit button.

i am following a tutorial in a book that said to put the script element in the head section. i realize if the variables are defined in the .js file it would work fine.

also i put the .js filename before the functions and i accidentally posted it before i removed it. here is the corrected code.


<i>
</i>&lt;head&gt;
&lt;script src="datetime.js" type="text/javascript"/&gt;
&lt;script type="terxt/javascript"&gt;
var timeStr = showDateTime();
var imgNum = getImage();
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;
document.write("&lt;img id='image' src='image" + imgNum + ".jpg' alt='' /&gt;");
&lt;/script&gt;
&lt;/body&gt;

Copy linkTweet thisAlerts:
@ominubauthorApr 07.2011 — doh!... can't self close script elements!
×

Success!

Help @ominub spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...