Google Answers Logo
View Question
 
Q: JS Script: Pop a page onUnload for 50% of the visitors ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: JS Script: Pop a page onUnload for 50% of the visitors
Category: Computers > Programming
Asked by: cclegg06-ga
List Price: $10.00
Posted: 20 Jan 2003 07:53 PST
Expires: 19 Feb 2003 07:53 PST
Question ID: 145923
I’m looking for a script that will do the following:

Called from a “onUnload”
Calculate a random number (between 0 and 1)
If random is <0.5 then:
- Randomly choose between three pages
- Display page via pop-up (over)

If random is >0.5 then do nothing.

Below is what I’ve developed thus far:

------------------------
// WINDOW DIMENSIONS //
function popitup(url)
{
	newwindow=window.open(url,'name','height=375,width=450,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
}
// SURVEY FUNCTIONS //
function chooseSurvey() {
};
var aRandom_number = matho.random()      //Random Number Generation
if (aRandom_number <= 0.5) {             //Incident Rate = 50%

homeAd = new chooseSurvey();
number = 0;
chooseSurvey[number++] = "<a src='...location1...'></a>
chooseSurvey[number++] = "<a src='...location2...'></a>
chooseSurvey[number++] = "<a src='...location3...'></a>

increment = Math.floor(Math.random() * number);
}

else {                                   //If not selected, do nothing
}
--------------

I’m looking for
- the “JS” file that I’ll call when the page is loaded
- the “onUnload” text to put in the <body> of the page

Thank you.
Answer  
Subject: Re: JS Script: Pop a page onUnload for 50% of the visitors
Answered By: theta-ga on 20 Jan 2003 09:26 PST
Rated:5 out of 5 stars
 
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
cclegg06-ga rated this answer:5 out of 5 stars
Thank you very much

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy