javascript - jQuery/ Ajax : How to call function on targeted page after redirect

标签 javascript jquery ajax redirect location

我是 Ajax 和一般编程的新手,希望有人能帮助我解决这个问题。

我在登录页面上使用以下内容。 这用于注册新用户,如果注册成功,将自动将他们转发到索引页面。

到目前为止,一切都按预期进行。 如何从此或之后在目标索引页上调用 jQuery 函数? 我想做的是向用户显示一个警报,告诉他们他们在成功注册后自动登录 - 所以 如果有人从登录页面重定向到索引页面,这个警报应该只显示在索引页面上.

我可以扩展下面的链接以传递一个变量(例如“&origin=login”),然后使用 PHP 在索引页上处理它,但这似乎是一个很大的解决方法,我想知道这是否可以实现使用 Ajax 更容易。

我做了一些研究并找到了一些关于 Ajax 的引用资料,但没有明确的指南。
有人可以帮我解决这个问题吗?

我的 Ajax(在 jQuery 中):

case 'registration successful':     
    $.ajax({        
        type: "post",   
        url: "ajax.php",
        cache: "false",
        data: {
            node: 'loginUser',
            email: email,
            pass: pass
        },
        success: function(data){
            // redirect to index page
            window.location.href = baseURL + '/index.php?lang=' + selectedLang;
            // after the redirect I would like to show an alert on the targeted index page
        }
    });
    break;

非常感谢, 迈克

最佳答案

如果我正确理解了你的问题,你可以使用 localStorage 在重定向后显示 alert,如下所示:

$.ajax({        
        type: "post",   
        url: "ajax.php",
        cache: "false",
        data: {
            node: 'loginUser',
            email: email,
            pass: pass
        },
        success: function(data){
            //set a localStorage item
            localStorage.setItem('showAlert','true');
            // redirect to index page
            window.location.href = baseURL + '/index.php?lang=' + selectedLang;
            // after the redirect I would like to show an alert on the targeted index page
        }
});

并在您的 index 页面中,在 script 标记或任何已加载的 js 文件中编写以下代码:

$(document).ready(function(){
    var showalert=localStorage.getItem('showAlert');
    if(showalert==='true')
    {
          //show the alert
          localStorage.setItem('showAlert','false'); //set its value back to false;
    }
});

关于 localStorage 的一些解释

  • It stores data with no expiration date.
  • Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance.

更新

对于您当前的要求,sessionStorage 可能很适合,因为您希望仅在用户登录时存储数据,并希望在用户注销时清除数据。用法类似于localStorage,您可以使用sessionStorage.setItem 代替localStorage.getItemlocalStorage.setItemsessionStorage.getItem

那么这个sessionStorage是什么

The sessionStorage object exists as a property of the window object in supporting browsers (currently Firefox 3+, Safari 4+, and Internet Explorer 8+). You can place data onto the sessionStorage object and that data persists for as long as that window (or tab) is open. Even if you navigate away from the page that stored the data and then navigate back, the data saved to sessionStorage is still available. Any data stored in sessionStorage is tied to the protocol, hostname, and port of the page that saved the information and only pages sharing the same protocol, hostname, and port can access that data later.

您可以阅读更多关于 sessionStorage here

关于javascript - jQuery/ Ajax : How to call function on targeted page after redirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129827/

相关文章:

NodeJS 中的 JavaScript 类和模块

javascript - 当特定列表项具有列表项内具有特定 href 的事件类时触发 jquery

javascript - 从 Bootstrap typeahead 和 JSON 制作可点击的结果列表

javascript - 如果没有结果,如何禁用 jquery ajax- 自动完成?

javascript - AJAX 响应中的空白

javascript - Firefox Javascript URL 重定向

javascript - 在使用 RSA-OAEP 在 JavaScript 中加密的 C# 中解密数据时出现 OAEP 填充错误

javascript - 文本不仅使背景层具有动画效果

javascript - 有人可以向其中注入(inject)恶意代码吗?

php - 如何在PHP分页中仅使用下一个和上一个按钮