[英]onclick: do something after changing location
假設我要執行以下操作:單擊“ div”時,更改到另一個位置(好像是...),然后執行一些操作。 我已經嘗試使用此代碼:
<div onclick="location.reload();location.href='...';togglebox('#ident')">Title</div>
其中ident
是新位置href='...'
對象的標識符,而togglebox
是腳本中定義的函數。 但是似乎瀏覽器嘗試在更改位置之前完成togglebox('#ident')
,因此輸出不是預期的。 例如,如果我嘗試
<div onclick="location.reload();location.href='...';console.log(5)">Title</div>
然后在更改位置之前會立即顯示5
,而我希望瀏覽器先更改位置然后顯示5
。
這個逆序解決方案有解決方案嗎?
歡迎任何建議
一種方法是使用查詢字符串告訴目標頁面要做什么。 但是,如果您想要一種可以完全滿足您要求的技術,則可以執行以下操作:
(這出現在您當前頁面的click事件處理程序中)
window.location.href = '...'; // Go to another page in your site
localStorage.setItem('exec',"yourCodeInQuotes()");
現在,目標頁面應該已經准備好處理您在localStorage
提到的ident
,或者直接評估代碼。 無論哪種方式,您都需要目標頁面來預測即將到來的數據。
(這在目標頁面中)
var message = localStorage.getItem('exec'); // Will get whatever you stored
eval(message); // Will attempt to run it as JavaScript
$('#'+message); // Or find it if it was your ident and do stuff...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.