【问题标题】:Display notification on target page after redirect重定向后在目标页面上显示通知
【发布时间】:2012-07-10 18:54:49
【问题描述】:

假设我在add-project.html 页面上有一个表单。请求完成后,我将用户重定向回主 index.html 页面。

我想要做的是在用户成功提交表单后index.html 上显示通知。

我正在使用这个插件来通知:http://redeyeoperations.com/plugins/Notify/

目前,我正在使用一个临时解决方案,它在提交表单时在add-project.html 上显示通知,然后在setTimeout 之后重定向到index.html 主页面,但我不喜欢它,它是很脏。

$('#printForm').submit(function(e) {
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: 'add-project.php',
        data: $('#printForm').serialize(),
        success: function(result) {
            if(result == '1') {
                // Success
                $.notify.basic('Congratulations, your projet has been saved correctly', {
                    occupySpace : true,
                    close : true,
                    autoClose: 5000
                });

                setTimeout(function() {
                    window.location.href = '../index.html';
                }, 2000);
            }
            else {
                // Error
                $.notify.error('Oops, something went wrong ! Make sure you filled all the necessary fields', {
                    occupySpace : true,
                    close : true,
                    autoClose: 5000
                });
            }
        }
    });         
});

有没有一种干净的方法可以在重定向后在目标页面上显示通知?

感谢您的帮助。

【问题讨论】:

  • 你为什么不尝试使用 ajaxLoad ?

标签: jquery ajax redirect notifications


【解决方案1】:

index.html 需要某种方式来知道表单已发送。您可以通过使用 url 参数或哈希并使用 js 读取它来做到这一点。

//after submit
window.location.href = '../index.html#success';

//in index.html
if(window.location.hash == '#success')
{
    //show the notification
}

【讨论】:

  • 非常好,这正是我想要的。谢谢!
  • @Thomas,每次重新加载页面都会显示通知。有没有办法只显示一次这个通知?
  • 非常感谢,这正是我的应用程序所需要的! :)
  • @Bagdat 我知道有点晚了.. 但是您可以在显示通知后清空哈希。我认为这会奏效。
  • @Thomas,是的,有点晚了)不过,谢谢你的回答!
猜你喜欢
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多