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

标签 jquery ajax redirect notifications

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

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

我正在使用此插件来发送通知:http://redeyeoperations.com/plugins/Notify/

目前,我使用一个临时解决方案,当表单提交后,它会在 add-project.html 上显示一条通知,然后重定向到主 index.html > 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
                });
            }
        }
    });         
});

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

感谢您的帮助。

最佳答案

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

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

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

关于jquery - 重定向后在目标页面上显示通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11427859/

相关文章:

php - 在加载 ajax 的页面内无限滚动

c# - jquery 拖放与 asp.net c# 功能

redirect - 阻止外部盗链的道德规范

c++ - 我怎么知道在 C++ 中 stdout 定向到哪里

javascript - # url 部分隐藏和显示

JQuery:高度不是函数

jquery - jQuery 中的调整按钮大小?

javascript - 删除部分除法的 onclick 监听器

Jquery:对同一 URL 的多个 ajax 调用的行为

javascript - 当网络显示 200 时处理 404