Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1195 How can I ask one in ten visitors if they want to carry out a survey?

You are here: irt.org | FAQ | JavaScript | Random | Q1195 [ previous next ]

It might be simpler to just do:

<script language="JavaScript"><!--
var nth = 10;
var rnd = Math.floor(Math.random() * nth) + 1;

if (rnd == nth) {
    if (confirm("Do survey?")) {
        location.href = 'survey.htm';
    }
}
//--></script>

If you must have a popup window then use:

<script language="JavaScript"><!--
var nth = 10;
var rnd = Math.floor(Math.random() * nth) + 1;

if (rnd == nth) {
     var windowHandle = window.open('popup.htm','windowName','width=400,height=400');
     if (!windowHandle.opener)
         windowHandle.opener = self;
}
//--></script>

then in popup.htm:

<script language="JavaScript"><!--
function yes() {
    opener.top.location.href = 'survey.htm';
    opener.focus();
    setTimeout('window.close()',10000);
}
//--></script>

Do survey?
<form>
<input type="button" value="Yes" onClick="yes()">
<input type="button" value="No" onClick="window.close()">
</form>

©2018 Martin Webb