0

Is it possible to run a js function\script after using already the windows.location.replace to get to a new website.

I need to use the script after the page load with the parameters I provide.

All my code is in html file running the scripts.

const today = new Date()// date
        var month =today.toLocaleString('default', { month: 'long' });// get the month 
        var day = today.getDate();//get the  Day
        var hour = today.getHours();//time 
        var month = today.getMonth() +1;
        var year = today.getFullYear();
        var filteredURL = `the website with the parameters`
        sessionStorage.setItem("reloading", "true");
        window.location.replace(filteredURL);

      window.onload = function() {
    var reloading = sessionStorage.getItem("reloading");
    if (reloading) {
        sessionStorage.removeItem("reloading");
        myFunction(console.log(test));
    }
}

// function reloadP() {
//     sessionStorage.setItem("reloading", "true");
//     document.location.reload();
// }

any help will be welcome. thanks in advance.

Try2prog
  • 161
  • 10
  • Do you own the redirected page? – Jean Will Dec 19 '21 at 16:51
  • 1
    Check this answer it is very good explained and working! https://stackoverflow.com/a/41905026/13885600 – Martin Dedja Dec 19 '21 at 17:17
  • the redirection is to another website. @MartinDedja I tried that but cant even console log after the reload, can I put my code maybe I did something wrong? – Try2prog Dec 19 '21 at 17:27
  • Do you own the other website? – Spankied Dec 19 '21 at 17:39
  • No, I dont own the other website, using same authenticated session – Try2prog Dec 19 '21 at 17:48
  • i even try https://stackoverflow.com/questions/7801910/jquery-code-not-called-after-window-location-replace-url but not sure how to implement it – Try2prog Dec 19 '21 at 17:59
  • @Try2Prog Per the restrictions you have given, of not owning the target page, such an approach cannot be used. See https://stackoverflow.com/a/38443144/2864740 for the basic premise why code can’t / won’t run after. Run any relevant code *before* redirecting. – user2864740 Dec 19 '21 at 19:51

0 Answers0