简体   繁体   English

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

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

I am new to Ajax and programming in general and hope someone can help me with this. 我是Ajax和程序设计的新手,希望有人可以帮助我。

I am using the following on a login page. 我在登录页面上使用以下内容。 This is used to register new users and will automatically forward them to an index page if their registration was successfully. 这用于注册新用户,如果注册成功,将自动将其转发到索引页面。

So far everything works as intended. 到目前为止,一切都按预期进行。 How can I call a jQuery function on the targeted index page from this or after that ? 我如何在此之后调用目标索引页面上的jQuery函数? What I am trying to do is to show the user an alert telling them they were automatically logged in after the successful registration - so this alert should only show up on the index page if someone gets redirected there from the login page . 我要执行的操作是向用户显示警报,告诉他们成功注册后他们已自动登录-因此, 如果有人从登录页面重定向到该索引页面,则该警报仅应显示在索引页面上

I could extend the below link to pass a variable (eg " &origin=login ") and then use PHP to deal with that on the index page but that seems to be a large workaround and I was wondering if this could be achieved easier using Ajax. 我可以扩展下面的链接以传递一个变量(例如“ &origin=login ”),然后使用PHP在索引页面上处理该变量,但这似乎是一个较大的解决方法,我想知道是否可以使用Ajax轻松实现。

I did some research and found some references to Ajax but no clear guideline for that. 我做了一些研究,发现了一些有关Ajax的参考资料,但没有明确的指导方针。
Can someone help me with this ? 有人可以帮我弄这个吗 ?

My Ajax (in jQuery): 我的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;

Many thanks in advance, Mike 预先感谢,迈克

If I understood your problem correctly you can make use of localStorage to display alert after redirecting as one below: 如果我正确理解了您的问题,则可以按以下方式重定向后,利用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
        }
});

and in your index page write below code in script tag or in any loaded js file: 并在您的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;
    }
});

A bit explanation about localStorage 关于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. 本地存储更安全,可以在不影响网站性能的情况下在本地存储大量数据。

UPDATE UPDATE

For your current requirement sessionStorage might suit well as you want to store the data only when the user logs it and want to clear it when he logs out. 对于当前的需求, sessionStorage可能非常适合,因为您只想在用户登录时存储数据,并在注销后才清除数据。 The usage is similar to localStorage , instead of localStorage.getItem or localStorage.setItem you can use sessionStorage.setItem or sessionStorage.getItem 用法类似于localStorage ,可以使用sessionStorage.setItemsessionStorage.getItem代替localStorage.getItemlocalStorage.setItem

So what is this sessionStorage 那么这个sessionStorage是什么

The sessionStorage object exists as a property of the window object in supporting browsers (currently Firefox 3+, Safari 4+, and Internet Explorer 8+). 在支持的浏览器(当前为Firefox 3 +,Safari 4+和Internet Explorer 8+)中,sessionStorage对象作为窗口对象的属性存在。 You can place data onto the sessionStorage object and that data persists for as long as that window (or tab) is open. 您可以将数据放置到sessionStorage对象上,并且只要打开该窗口(或选项卡),该数据就会保留。 Even if you navigate away from the page that stored the data and then navigate back, the data saved to sessionStorage is still available. 即使您离开存储数据的页面然后再浏览回来,保存到sessionStorage的数据仍然可用。 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中存储的任何数据都与保存该信息的页面的协议,主机名和端口相关,只有共享相同协议,主机名和端口的页面才能稍后访问该数据。

You can read more about sessionStorage here 您可以在此处阅读有关sessionStorage更多信息

you can read the query string in JS (you dont have to use PHP for that). 您可以在JS中读取查询字符串(不必使用PHP)。

i have used this function before: 我以前使用过此功能:

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];
        }
    }
}

using localStorage may result it problems in older browsers. 在较旧的浏览器中,使用localStorage可能会导致问题。 if you have to avoid using the query string i suggest that you will use a cookie. 如果您必须避免使用查询字符串,我建议您使用cookie。

this should show you how 这应该告诉你如何

        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....");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM