...

View Full Version : Calling script from a html page



jdsflash
12-12-2009, 04:19 PM
I have to call the script below from my html page. How would I do that? Do I need to modify the script at all? Put in a function that returns the random title to the html page?

New to javascript any help woudl be apreciated.

html page that calls the script


<html>
<head>

<script type='text/javascript' src='titles.js'></script>
<script>
document.title = rantitle[ranNumber];
</script>

</head>



titles.js


<script type="text/javascript" language="javascript">
<!--//
if (parseInt(navigator.appVersion) >= 4) {
var num = 3;
var rantitle = new Array(num+1);
rantitle[1] = "Random Title1";
rantitle[2] = "Random Title1";
rantitle[3] = "Random Title1";
ranNumber = parseInt(num * Math.random() + 1);
document.title = rantitle[ranNumber];
}
//-->
</script>

Philip M
12-12-2009, 04:29 PM
Your .js file must not contain any HTML tags so delete

<script type="text/javascript" language="javascript">
<!--//

//-->
</script>

if (parseInt(navigator.appVersion) >= 4) { is quite redundant. IE3 and NS3 have long been one with Nineveh and Tyre.

<script>
document.title = rantitle[ranNumber];
</script>
is also redundant. The .js script returns the value of document.title already.


<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
The <!-- and //--> comment (hiding) tags have not been necessary since IE3. If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

To summarize:-


<html>
<head>
<script type='text/javascript' src='titles.js'></script>
</head>

The .js file:-


var num = 3;
var rantitle = new Array(num+1);
rantitle[1] = "Random Title1";
rantitle[2] = "Random Title1";
rantitle[3] = "Random Title1";
ranNumber = parseInt (num * Math.random() + 1);
document.title = rantitle[ranNumber];

vwphillips
12-12-2009, 05:26 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

// this can go in a external js
function Title(){
var rantitle = [];
rantitle[0] = "Random Title1";
rantitle[1] = "Random Title2";
rantitle[2] = "Random Title3";
ranNumber = parseInt (rantitle.length*Math.random());
document.title = rantitle[ranNumber];
}

Title();

/*]]>*/
</script></head>

<body>
<input type="button" name="" value="Test" onclick="Title();"/>
</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum