Anan Srivastava
Anan Srivastava

Reputation: 112

Click function in Jquery on different .aspx pages

I have two pages: index.aspx and dream.aspx. I have a button click me on index.aspx Here's my Jquery code that clicks on that button:

function createItemButton()
    {
        setTimeout(
        function()
        {
            $("#CreateItem").click();
        },5000);    //Delay of 5 seconds


    }
createItemButton();

The delay added is because of the rest of the code not posted here. Once the click me button is triggered, it takes me to the dream.aspx page. There's another button back on that page, which when clicked, takes me back to the index.aspx page. Here's my attempt to automate everything:

function createItemButton()
{
    setTimeout(
    function()
    {
      $("#CreateItem").click(); //id of click me button on index.aspx page
    },5000);    //Delay of 5 seconds


 }

function createItemButton1()
{
    setTimeout(
    function()
    {
      $("#back").click();  //id of back button on dream.aspx page
    },7000);    //Delay of 7 seconds


 }
 function call()
 {
    createItemButton();
    createItemButton1();
 }
 call();

My thinking is that once it redirects to the dream.aspx page, it'll wait another 2 seconds ( 7000 - 5000 delay ) and the trigger a click on the back button and take me back to the index.aspx page. But for some reason, after it is taking me to the dream.aspx page, it's not triggering a click on the back button on the same page. I have tried giving multiple delays in the same setTimeout() function also. Something like:

function(){
    setTimeout(function() {
        $("#CreateItem").click();
        $("#back").click();
    }, 3000));
}

I have been informed that this code will give a delay of 3 seconds to each of the clicks. But it did not work for me. I also tried a couple of other things but nothing worked.

Basically, once I move from the index.aspx page after clicking on the click me button, I want the back button on the dream.aspx page to take me back to the index.aspx page after a couple of seconds. All automated. FYI, I am running this code in console. Any help is much appreciated. Thanks.

Upvotes: 1

Views: 69

Answers (1)

Igor
Igor

Reputation: 15893

The code for back button (function createItemButton1()) and its call must be placed in dream.aspx. Once you leave index.aspx, all javascript code in it is gone along with any pending timeouts.

Upvotes: 2

Related Questions