jQuery / Ajax: 如何在重定向后调用目标页面上的函数

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

我不熟悉 Ajax 和一般的编程,希望有人能帮助我。

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

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

我可以扩展下面的 link 以传递一个变量(例如“&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.setItemsessionStorage.getItem

而不是 localStorage.getItemlocalStorage.setItem

所以这是什么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

        you can use query string as well for this purpose
        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+'&msg=true';
                    // after the redirect I would like to show an alert on the targeted index page
                }
            });
            break;
    ---------------------
On index page get value from url
$(document).ready(function(){
    var msg=window.location.toString().Split('msg')[1];
    if(msg)
    {
          alert("Welcome....");
    }
});

您可以在 JS 中读取查询字符串(您不必为此使用 PHP)。

我以前用过这个功能:

function GetQueryStringParams(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}

使用 localStorage 可能会导致它在旧版浏览器中出现问题。如果您必须避免使用查询字符串,我建议您使用 cookie。

this should show you how