weixin_33736048 2015-09-30 22:19 采纳率: 0%
浏览 431

onbeforeunload不会触发

I have an onbeforeunload event which should fire when I leave the page and run the code I have attached to that event. I have placed this within the scripts tag of the cshtml in my page, so in theory it should only fire if I am on that particular page and move off it(my understanding of the event could be incorrect). However when I go to another page the onbeforeunload event does not seem to want to fire. I have tried setting a breakpoint on it but does not seem to hit it and am not getting any errors on my console in firebug.

I have looked at this post which one of the posters mentioned using this event to detect a page change Best way to detect when a user leaves a web page?

<script type="text/javascript">

$(document).ready(function () {

    window.onbeforeunload = SaveDashboard;


});

function SaveDashboard(){
    var gridArray = _.map($('.grid-stack .grid-stack-item:visible'), function (el) {
        el = $(el);
        var gridID = el.find('.grid-stack-item-content.ui-draggable-handle').first().attr('id');
        var node = el.data('_gridstack_node');
        return {
            id: gridID,
            x: node.x,
            y: node.y,
            width: node.width,
            height: node.height
        };
    });

    $.ajax({
        url: 'Dashboard/EditWidgets/',
        type: 'PUT',
        data: {
            widget: gridArray
        },
        success: function (dataset) {
        },
        failure: function (xhr, error) {
            console.log(xhr)
            console.log(error)
        },
    });
}
</script>
  • 写回答

1条回答 默认 最新

  • 三生石@ 2015-09-30 22:52
    关注

    you should return a string in the onbeforeunload handler. if you don't return anything, the event won't fire your handler. the text you have to return is the text that will be placed in the prompt the browser will open asking if the user really wants to leave the page.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。