/    Sign up×
Community /Pin to ProfileBookmark

calling scripts inside body tags?

i wrote my entire webpage using the document.write command, but now i need to create a functino inside the body tags, is this possible, and if so how on earth is it done, thank you

to post a comment
JavaScript

13 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonNov 17.2003 — May I suggest that you don't write your entire page in JavaScript, if you do that then people with JavaScript disabled or people using non-visual browsers cannot "view" your page.

In any case, here's how it's done:

document.write('<script type="text/javascript"><!--');

document.write('a_function();');

document.write('</sc'+'ript>');
Copy linkTweet thisAlerts:
@fredmvNov 17.2003 — My preferred method (escaping closing [font=courier]<script>[/font] tag):

[font=courier]

document.write('<script type="text/javascript"><!--');

document.write('a_function();');

document.write('[b][color=blue]//-->[/color][/b]</script>');[/font]
Copy linkTweet thisAlerts:
@David_HarrisonNov 17.2003 — Oops, forgot the //-->
Copy linkTweet thisAlerts:
@fredmvNov 17.2003 — [i]Originally posted by lavalamp [/i]

[B]Oops, forgot the //--> [/B][/QUOTE]
Yeah, that's why I made it bold and blue. ?
Copy linkTweet thisAlerts:
@David_HarrisonNov 17.2003 — I noticed.
Copy linkTweet thisAlerts:
@CharlesNov 17.2003 — [font=georgia]What's up with the HTML comments?[/font]
Copy linkTweet thisAlerts:
@David_HarrisonNov 17.2003 — It's to hide the script for browsers with JavaScript disabled. I would have thought that you would have known that one Charles.
Copy linkTweet thisAlerts:
@fredmvNov 17.2003 — [i]Originally posted by Charles [/i]

[B][font=georgia]What's up with the HTML comments?[/font] [/B][/QUOTE]
I believe it's used since, in browsers that don't support JavaScript, they actually print out all of the code in-between the [font=courier]<script>[/font] tags, by commenting them out prevents this. A lot of people also use this same technique for internal CSS.
Copy linkTweet thisAlerts:
@CharlesNov 17.2003 — [font=georgia]Then why are you using it inside of a "document.write()"?[/font]
Copy linkTweet thisAlerts:
@David_HarrisonNov 17.2003 — Good point, habit I suppose. It's good practice for when it is needed though.
Copy linkTweet thisAlerts:
@CharlesNov 17.2003 — [i]Originally posted by lavalamp [/i]

[B] It's good practice for when it is needed though. [/B][/QUOTE]
[font=georgia]But dangerous in this case. Consider...

[font=monospace]<script type="text/javascript">

<!--

document.write('<script type="text/javascript"><!--');

...

document.write('// --></script>');

// -->

</script>[/font]

Now, SGML has a lot of rules the browsers ignore and one of them is the permissibility of nested comments. And the browsers that do not understand the SCRIPT element are the ones that you can expect to have trouble with nested comments. To such a browser the above looks like...

[font=monospace]<!--

document.write('<!--');

...

document.write('// -->');

// -->[/font][/font]
Copy linkTweet thisAlerts:
@David_HarrisonNov 17.2003 — Best just to stick with this then:

document.write('<script type="text/javascript">');

document.write('a_function();');

document.write('</sc'+'ript>');

or:

document.write('</script>');

A good spot Charles. How are you not a moderator yet?
Copy linkTweet thisAlerts:
@fredmvNov 17.2003 — [i]Originally posted by lavalamp [/i]

[B]A good spot Charles. How are you not a moderator yet? [/B][/QUOTE]
I've always thought he was...
×

Success!

Help @drmbdrummer 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,
)...