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

mor*_*rgi 5 ajax jquery notifications redirect

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

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

我正在使用此插件进行通知:http://redeyeoperations.com/plugins/Notify/

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

$('#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
                });
            }
        }
    });         
});
Run Code Online (Sandbox Code Playgroud)

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

感谢您的帮助。

Tho*_*mas 5

index.html 需要某种方式来知道表单已发送。您可以通过使用 url 参数或哈希并使用 js 读取它来完成此操作。

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

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