当用户在阅读产品价格后尝试退出页面时,我想向他们提供第一折扣。
如果他们选择'YES'他们购买产品而非关闭窗口,如果他们选择'否'那么我会做第二次折扣 优惠,这次是最后一次优惠。
如果他们第二次再次点击'否',他的浏览器就会 关闭,否则他会以给定的第二折扣购买产品 价。
我的示例如下所示:
<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>
我的问题是,当用户尝试退出(或)刷新页面时,如何触发此功能。
我已经重写了代码并试图修复恼人的页面加载弹出窗口,有人可以建议我吗?
<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>
我正在尝试传递全局变量 'firstTime' 以检查它是否来自onLoad然后如果来自onUnload然后显示商品,则不显示商品。有没有解决这个问题的方法?
答案 0 :(得分: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;
}
玩弄它。
答案 1 :(得分:3)
<body onunload="OnUnload()">
然后
function OnUnload()
{
//put code here
}