javascript - Can I run a function after navigating to a new page and waiting for that page to finish loading? - Stack Overflow

I have a script that executes a function following a click event. This function sets cookies and redire

I have a script that executes a function following a click event. This function sets cookies and redirects to a new URL. I would like to delay the execution of another function until the new URL has fully loaded. How can I achieve this?

At the moment the script basically looks like this:

function setAndNavigate(event){

    event.preventDefault()

    //code setting cookies

    //navigating to new url
    window.location.href = ";
}

//click event listener runs the setAndNavigate function
var link = document.getElementById("launch");
link.addEventListener("click", setAndNavigate);

//using navigate event listener to kick off function once page url changes
navigation.addEventListener("navigate", (event) => {
        this.newFunction();
    });

//new function
function newFunction(){
    //new function code
}

I am using the navigate event listener to check for a navigation event and then running the code within that. However, at the moment, the new function runs before the new page has finished loading. I want the code to run on the new page once it has finished loading the UI.

This is a test environment and I don't have access to the application code.

I have a script that executes a function following a click event. This function sets cookies and redirects to a new URL. I would like to delay the execution of another function until the new URL has fully loaded. How can I achieve this?

At the moment the script basically looks like this:

function setAndNavigate(event){

    event.preventDefault()

    //code setting cookies

    //navigating to new url
    window.location.href = "http://newurl"
}

//click event listener runs the setAndNavigate function
var link = document.getElementById("launch");
link.addEventListener("click", setAndNavigate);

//using navigate event listener to kick off function once page url changes
navigation.addEventListener("navigate", (event) => {
        this.newFunction();
    });

//new function
function newFunction(){
    //new function code
}

I am using the navigate event listener to check for a navigation event and then running the code within that. However, at the moment, the new function runs before the new page has finished loading. I want the code to run on the new page once it has finished loading the UI.

This is a test environment and I don't have access to the application code.

Share Improve this question asked Nov 20, 2024 at 12:09 Kieran MKieran M 4617 silver badges17 bronze badges 2
  • I assume this is all on the same origin? If so, use an iFrame and trigger onload of that – mplungjan Commented Nov 20, 2024 at 12:45
  • on the new page check the cookies (or querystring or localStorage) and if condition is right then execute newFunction() which must be defined in the new page. – IT goldman Commented Nov 20, 2024 at 13:33
Add a comment  | 

1 Answer 1

Reset to default 1

So basisclly you need to change

navigation.addEventListener("navigate", (event) => {
        this.newFunction();
    });

to "http://newurl" js script files and use

document.addEventListener('DOMContentLoaded',(event) => {
        this.newFunction();
    })

DOMContentLoaded event will trigger if your content in "http://newurl" page is completely load dont't fet to move

newFunction();

to your new page script too.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742358767a4428964.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信