0

When a user tries to exit the page after reading price of the product, i want to offer them 1st discount.

  1. If they select 'YES' they buy the product instead of closing the window, if they select 'NO' then i would make 2nd discount offer and this time it will be the last offer.

  2. If they click 'NO' again even second time, his browser will close or else he would buy the product at the given 2nd discount price.

My Example is illustrated below:

<script type="text/javascript">
function getOffers()
{
  var question1 = confirm("Wait! we are now offering this Product in $25.00, buy it?");
  if(question1 == 'true')
  {
        alert('You have purchased this product for $25.00');
  }
  else
  {
        var question2 = confirm("Wait! we would like to make one Final Offer, price of Product in $15.00, buy it?");
        if(question2 == true)
        {
              alert('You have purchased this product for $15.00');
        }
        else
        {
              //Close this window
        }
  }
}
</script>

My question is, how can i trigger this function when user tries to exit (or) refresh the page.

Additional INFO:

I have rewritten the code and trying to fix the annoying page load popup, can any one suggest me on this?

<script type="text/javascript">
function getOffers()
{
  //alert(firrsTime);
  if(firstTime == 'no')
  {
      var question1 = confirm("Wait! we are now offering this Product in $25.00, buy it?");
      if(question1 == 'true')
      {
            alert('You have purchased this product for $25.00');
      }
      else
      {
            var question2 = confirm("Wait! we would like to make one Final Offer, price of Product in $15.00, buy it?");
            if(question2 == true)
            {
                  alert('You have purchased this product for $15.00');
            }
            else
            {
                  //Close this window
            }
      }
  }
}

window.onunload = function(){
    firstTime = 'no';
    getOffers();
}
window.onload = function(){
    firstTime = 'yes';
    getOffers();
}
</script>

I am trying to pass a global variable 'firstTime' to check if it is from onLoad then dont display offer if it is from onUnload then display offer. Is there any solution on this problem?

4

2 回答 2

3

使用 onunload 和 onload。

window.onunload = function(){getOffers();}
window.onload = function(){getOffers();}

虽然它会在用户首次加载页面时显示,所以我不建议这样做。

关于区分首次加载和刷新的方法,还没有尝试过,但我在某处找到了这段代码:

    function OnCrmPageLoad()
{
   //bind to the onsave event
   crmForm.attachEvent(“onsave”,OnCrmPageSave);

   if (crmForm.new_ispostback.DataValue ==  true)
   {
           //form was saved
           //reset the field
            crmForm.new_ispostback.DataValue = false;
   }
}

function OnCrmPageSave()
{
    // validate form if needed
    crmForm.new_ispostback.DataValue = event.returnValue = true;
    crmForm.new_ispostback.ForceSubmit = true;
     return true;
}

玩弄它。

于 2012-05-16T19:46:14.983 回答
3
<body onunload="OnUnload()">

然后

function OnUnload()
{
   //put code here
}
于 2012-05-16T19:46:29.443 回答