如果在另一个页面上发现文本,则发出Javascript警报

Javascript alert if text is found on another page

本文关键字:警报 Javascript 文本 发现 另一个 如果      更新时间:2023-09-26

当前使用此代码显示一个弹出,如果文本是在该页上找到。但是,如果文本存在于原始页面上,我想在其他页面上显示该弹出框。

的例子文本在/发票上我导航到/付款弹出窗口仍在运行

<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>

对于简单的解决方案,您可以通过在本地存储中保存此标志并访问每个页面来检查localstorage是否有值,然后显示弹出框,否则没有。在1小时的时间跨度之后,应该清除本地存储。所以现在你在页面上的作用是当你的元素现在是

 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");
        }
      }

和所有其他页

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");
    }
 }

最好的方法是使用Ajax HTTP GET提供URL并在响应字符串中搜索。

https://api.jquery.com/jquery.get/

alert默认为window.alert;JavaScript正在运行的当前窗口的本地警报方法。

所以,如果你想在另一个窗口中发出警报(你可能已经用window.open打开了),你必须使用那个窗口对象:

otherWindow.alert(...);