M.Heredia M.Heredia - 3 months ago 15
Javascript Question

Javascript alert if text is found on another page

Currently using this code to show a popup if the text is found on that page. However, I wanted to show that popup on other pages if the text exists on the original page.

Example
Text is on /invoice
I navigate to /payment
Pop up still runs

<script type="text/javascript">
window.onload = function(){
//If the body element of the page contains 'one hour turnaround' then trigger an alert
if(document.body.innerHTML.toString().indexOf('one hour turnaround') > -1){
alert("You have a ONE HOUR TURNAROUND order");
}};
</script>

Answer

For easy workaround, you can do this by saving the flag for this in the local storage and accessing on every page to check if that localstorage has value then show the popup otherwise not. after that 1hour time span should clear the localstorage. so now you dunction on the page wher you have that element is now

 window.onload = function(){
        //If the body element of the page contains 'one hour turnaround' then trigger an alert
        if(document.body.innerHTML.toString().indexOf('one hour turnaround') > -1){
              localStorage.setItem('oneHourTurnaround', "true");
               alert("You have a ONE HOUR TURNAROUND order")
               setTimeout(function(){ alert("You have a ONE HOUR TURNAROUND order"); }, 3000); // currenlty it's 3 sec
          }
        else{
            localStorage.setItem('oneHourTurnaround', "false");
        }
      }

and for all other page are

window.onload = function(){
    //If the body element of the page contains 'one hour turnaround' then trigger an alert
    if(localStorage.getItem('oneHourTurnaround')==true){
      alert("You have a ONE HOUR TURNAROUND order");
    }
 }