Hi cclegg06-ga,
Let us start with the HTML you will put in the page. To use a
separate .js file that you want to call in your code, you can use the
<SCRIPT> tag, with its src property set to the filename, as explained
here :
- About.com : JavaScript Glossary - External JS File
( http://javascript.about.com/library/gloss/blexternaljs_def.htm)
To call your chooseSurvey() function whenever a page unloads, call
the function in the <BODY> tag's onUnload event, like this:
<BODY onUnload="chooseSurvey();">
Here is an HTML page template that references a separate javascript
file, 'rndSurvey.js', and calls the function chooseSurvey() contained
in it :
========= BEGIN HTML FILE =========
<html>
<head>
...
<script src="rndSurvey.js">
</script>
...
</head>
<body onUnload = "chooseSurvey();">
...
</body>
</html>
========= END HTML FILE ==========
I give the contents of the rndSurvey.js file below. The file contains
the popitup() function specified by you, and a slightly modified
version of your chooseSurvey() function.
========= BEGIN rndSurvey.js ==========
// WINDOW DIMENSIONS //
function popitup(url)
{
newwindow=window.open(url,'name','height=375,width=450,scrollbars=yes');
if (window.focus) {newwindow.focus()}
}
// SURVEY FUNCTION //
function chooseSurvey()
{
var aRandom_number = Math.random(); //Random Number Generation
if (aRandom_number <= 0.5)
{
//Incident Rate = 50%
// Initialize vars
var homeAd = new Array(); // homeAd is an array of URLs
var number = 0;
// Set the Urls
homeAd[number++] = "://www.google.com/ ";
homeAd[number++] = "http://www.yahoo.com/ ";
homeAd[number++] = "http://www.altavista.com/ ";
// Randomly select one of the above page locations
var increment = Math.floor(Math.random() * number);
// Call the popup window function
popitup(homeAd[increment]);
} // end if
// end function
}
========= END rndSurvey.js ==========
If you change the name of the JavaScript file or functions, be sure to
make the relevant changes in the HTML file.
Hope this helps.
If you need any clarifications, just ask!
Regards,
theta-ga
:)
=========================================
Google Search Terms Used : javascript separate file js |