![](/img/trans.png)
[英]how to pass variable from one function to another function in javascript
[英]how to pass local variable from one function to another function in javascript
我需要將此變量從一個函數傳遞到另一個函數,但是與此有關。
var url;
var ua = navigator.userAgent.toLowerCase();
$("#ToBuyStep2").click(function(){
if(ua.match(/MicroMessenger/i)=="micromessenger") {
url=window.location.href;
window.location.href = "http://www.juliyeah.com/wap/tmpl/order/weixin.html";
}
})
function goBackUrl(){
if(ua.match(/MicroMessenger/i)!="micromessenger") {
window.location.href = "http://www.juliyeah.com";
}
}
因此,我需要在單擊ToBuyStep2
時記住該URL,並將該URL傳遞給goBackUrl()
函數。 這兩個函數在不同的文件中調用,因此我無法將它們組合在一起。 有誰知道如何做到這一點?
const functionGenerator = function() {
let url = '';
let result = {};
result.ToBuyStep2 = function() {
// you can access URL here
};
result.OnClick = function() {
// you can access URL here
};
return result;
};
let temp = functionGenerator();
document.getElementById('whatever').onclick = temp.OnClick;
document.getElementById('stuff').someEvent = temp.ToBuyStep2;
您可以使用sessionStorage設置“最后一個URL”,然后在boGackUrl函數中請求它: sessionStorage.setItem('prevUrl', window.location.href)
。 請記住,更改位置后,您的數據將丟失。 但是將數據傳遞到會話存儲中將使數據可用,直到用戶關閉選項卡/瀏覽器為止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.