抱歉延迟回答,但我想写一个好方法,而不仅仅是快速回答积分。仅当页面都在同一个域下时,这才有效。 LocalStorage 就是这样工作的。让我知道这是否适合您,并且可能需要稍作调整,但应该给您一个工作的想法。可能有更好的方法,但我一开始就是这样处理的。
这将在每一页上进行。然后,您可以使用页面 url 或类似隐藏字段之类的东西来使每个页面都独一无二,并且可以在您当前访问该页面时进行跟踪。但总体思路应该可行。
编码愉快!希望对您有所帮助!
这是有效的,因为该脚本在每个页面上运行。它知道它在哪个页面上的唯一原因,
是由于脚本执行时隐藏字段值表示当前页面的原因。
这是一个非常普遍的例子。但应该给你的概念。
由于此脚本在每个页面上运行,并且是通用编写的,因此可以解决。只需确保触发事件的按钮名称在每一页上都相同!
附:您还可以使用 window.location 来获取您所在的页面而不是隐藏字段。
****每页的HTML示例*****
<!-- Each Page -->
<!-- Page 1-->
<div>
<input type="hidden" value="page1"/>
<input type="button" id="cmdButtonThatTriggersEvent" value="Test Buttom"/>
</div>
<!-- Page 2-->
<div>
<input type="hidden" value="page2"/>
<input type="button" id="cmdButtonThatTriggersEvent" value="Test Buttom"/>
</div>
<!-- Page 3-->
<div>
<input type="hidden" value="page3"/>
<input type="button" id="yourButton" value="Test Buttom"/>
</div>
//Make sure ever button name or id is the same on each page.
//That way it is generic. You will also have to track what page //triggered the click event. So make a localstorage hold if the action was clicked to occur, and a localstorage key that holds an array fo all the pages you want this done on. So that way the code can be on every page, and you mark the page off if all page entries were affected, then reset the switch back to null.
****Javascript example for each page*****
<script>
var globalAllPages = ["Page1, "Page3", "Page3"]; //etc.
$(document).on("click", "#yourButton", function(){
localStorage.setItem("trigger_state", "true");
HandleLocalStorageSwitch();
});
$(document).ready(function(){
setInterval(function(){
HandleLocalStorageSwitch();
}, 500);
});
function HandleLocalStorageSwitch()
{
var trigger = localStorage.getItem("trigger_state");
var location = localStorage.getItem("location");
if(trigger != undefined && trigger == "true"){
//now see if this page has ran yet
var dictPages = JSON.parse(localStorage.getItem("current_pages")); //gets the array
var exists = false;
for(var x=0;x<dictPages;x++){
if(dictPages[x]=="this_page"){
exists=true;
break;
}
}
//if it wasnt found then insert it
if(!exists){
dictPages.add("this_page");
$("#this_page_Button").click(); //now trigger this button
}
if(dictPages.length == globalAllPages.length){
//we hit all pages
//then all have been ran, and remove the switch and clear dict
localStorage.setItem("shouldTriggerEvent", "false");
//so now reset and won't run untill one of buttons clicked, then starts process again
localStorage.removeItemKey("current_pages");
}
}
}
}