如何通过onload将动态URL传递给函数

时间:2017-02-05 16:49:35

标签: javascript jquery onsen-ui

我需要一个肝脏,我试图将动态网址传递给一个函数。

onload="getImage('+ val.logo +');" onclick="loadPageMerchantInfo('+ val.merchant_id +');

val.logo拥有这样的网址:{{3}}

我需要将此网址传递给

以下的功能
function getImage(url)
{
setStorage("merchant_logo",url);
var logo = getStorage("merchant_logo");
onsenAlert(logo);
return logo;
}

function loadPageMerchantInfo(str)
{

var merlogos = getImage();
onsenAlert(merlogos);
setStorage("merchant_id",str);


var options = {
  animation: 'none',
  onTransitionEnd: function() {                   
      displayMerchantLogo2( 
             merlogos,
             '' ,
             'page-merchantinfo'
      );                  
      callAjax("getMerchantInfo","merchant_id="+ getStorage('merchant_id'));              
  } 
};  
sNavigator.pushPage("merchantInfo.html", options);     
merhantPopOverMenu.hide(); 
}

因此,当我打印该值时,没有任何内容,只显示“未定义”。

1 个答案:

答案 0 :(得分:0)

在html中:

onload="getImage(val.logo);";

js内部:

window.onload=()=>getImage(val.logo);
window.onclick=()=>loadPageMerchantInfo(val.merchant_id);

您可以查看箭头功能(正常情况也可以)和闭包。

为什么你的代码不起作用:

window.onload="a string";
//the js engine does:
window.onload();
//wich will cause an error:
ERROR: window.onload is not a function!

如果你真的想要这个糟糕的样式字符串,你需要将String转换为一个函数:

onload=new Function("getImage("+ val.logo +");");
onclick=new Function("loadPageMerchantInfo("+ val.merchant_id +");");

但是,每个JSlover都会因此而杀了你。