在重定向后在目标页面上显示通知

5
假设我在“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呢? - Fredy
1个回答

5

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
非常感谢,这正是我在应用程序中所需要的! :) - Alexander
@Bagdat 我知道有点晚了.. 但在显示通知后,你可以清空哈希表。我认为那会有效。 - Alexander
@Thomas,是的,有点晚了)不过,还是谢谢你的回答! - Bagdat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接