'Javascript on two pages

I'm trying to create a jquery script.

The script need to :

1- Click for a specific link (button) on a list in order to change the page

2- Fill out the text input on the page

3- Valid

(4- and loop...)

The problem is, when I change the page, the script doest execute on the second page.

Here is my script :

window.allButtons = $('a[href^="/discussions/"]')

if ($(allButtons[2]).text()=="Afficher cette discussion"){
    allButtons[2].click();
}

setTimeout(function()

{

    let messages = [
    'Bonjour, je suis désolé mais j\'ai déjà envoyé mon colis avec weeship'
    ];
    function getMessage() {
       return messages[Math.floor(Math.random() * messages.length)];
    }

    $('#message_content').val(getMessage);

    // setTimeout(function(){ 
    //  $('input.button.button--tertiary').click();
    // },1000);

},1000);

Please, what's the solution?



Solution 1:[1]

You can use Session storage to hold on data from different pages.

Before you leave the page you could set a value as an indicator or the value you actually want with something like that:

sessionStorage.setItem('key', 'value');

And once the second page load retrieve the necessary information from session storage with something like that

// Get saved data from sessionStorage
var data = sessionStorage.getItem('key');

You can apply your logic now. If the value wasRedirected for example is true it means he came from your redirect so you can check if you have a stored value for the key theInputValue and set it to the input.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anastasios Selmani