How can I execute a script after calling [removed].href?

前端 未结 8 1471
执笔经年
执笔经年 2020-12-03 10:48

I have a script that redirects the user to another page. I want to load some content into a div on the new page after the new page has fully loaded. How can I do this. The f

8条回答
  •  情话喂你
    2020-12-03 11:04

    An easy way to pass data to your page you are redirecting to would be to set some url parameters.

    For example:

    window.location.href - "http://youpage.com/?key=value"
    

    When that page loads you could have a:

    $(document).ready(function(){
    
        var my_param = getUrlParameter('key');
        if(my_param == "value"){
    //do your stuff here
    }
    
    });
    
    
    var getUrlParameter = function getUrlParameter(sParam) {
        var sPageURL = decodeURIComponent(window.location.search.substring(1)),
            sURLVariables = sPageURL.split('&'),
            sParameterName,
            i;
    
        for (i = 0; i < sURLVariables.length; i++) {
            sParameterName = sURLVariables[i].split('=');
    
            if (sParameterName[0] === sParam) {
                return sParameterName[1] === undefined ? true : sParameterName[1];
            }
        }
    };
    

提交回复
热议问题