/    Sign up×
Community /Pin to ProfileBookmark

Pls help newbie to include javascript in html head tag

Hi Javascript experts,
I downloaded this javascript off the internet to colour alternate rows of a table, and just had to add this script tag to the head of my html file for it to work (“<script type=”text/javascript” src=”tableDecorate.js”></script>”). However, can anyone tell me how I can include the javascript (given below) in the html source itself, rather than calling the external js file? Its probably pretty simple, but since I don’t know anything about javascript, I don’t have a clue how to do it. Please help!

//tableDecorate.js
// alternately you can place these in a script element..
var tableDecorate_rows = true;
var tableDecorate_columns = true;

function tableDecorate(e) {
if (!tableDecorate_rows && !tableDecorate_columns) return;
if (!document.getElementsByTagName) return;
var counter,
c, // columns
r, // rows
t; // tables
t=document.getElementsByTagName(“table”);
for (var i=0; i<t.length; i++) {
r=t[i].getElementsByTagName(“tr”);
for (var j=0; j<r.length; j++) {
// add “oddRow” class to odd rows
if (tableDecorate_rows && (j % 2 == 1)) {
r[j].className = “oddRow ” + r[j].className;
}
if (tableDecorate_columns) {
c=r[j].childNodes;
counter = 0;
for (var k=0; k<c.length; k++) {
// node must be element, not whitespace
// add “oddCol” class to odd cells
if ((c[k].nodeType == 1) && (counter++ % 2 == 1)) {
c[k].className = “oddCol ” + c[k].className;
}
}
}
}
}
}

// alternately you can call the function directly (after onload)..
if (window.attachEvent) {
window.attachEvent(‘onload’,tableDecorate);
} else if (window.addEventListener) {
window.addEventListener(‘load’,tableDecorate,false);
}

Thanks a lot,
Munna

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rhsundergroundMar 17.2004 — [code=php]
<html>
<head>
<title>this page</title>
<script>
[B][I]INSERT SCRIPT HERE[/I][/B]
</script>
</head>
<body>
blah blah blah
</body>
</html>
Copy linkTweet thisAlerts:
@PittimannMar 17.2004 — Hi!

Just put this:

<script language="JavaScript" type="text/javascript">

<!--

above the code from the js file and this:

//-->

</script>

below. Than it should work.

Cheers - Pit

Edit: rhsunderground was faster! Anyway - it is better, not to use the "naked" <script> tag... ?
Copy linkTweet thisAlerts:
@SamMar 17.2004 — rather than this:<script type="text/javascript" src="tableDecorate.js"></script>

use this:
[code=php]<script type="text/javascript">
<!--Cut and paste tableDecorate.js here-->
</script>[/code]

Ya beat me pit...
×

Success!

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