JS Script: Pop a page onUnload for 50% of the visitors

January 6, 2009
  • 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++] = " chooseSurvey[number++] = " chooseSurvey[number++] = " 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 of the page Thank you.


  • 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 ... ... ========= 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







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about JS Script: Pop a page onUnload for 50% of the visitors , Please add it free.