javascript变量替换this.refresh,window.refresh

时间:2012-09-27 10:01:09

标签: javascript android html ios cordova

在我的手机间隙index.html javascript部分,window.refresh将用于IOS和this.refresh用于android。我想为两者使用相同的index.html。通过(navigator.userAgent.toLowerCase()。match(),我可以区分IOS和android,但我不想在每个时间窗口应用这个条件。或者这个。可以使用。我可以全局应用这个条件并为一个变量分配值,即'this'用于android和'window'用于IOS,然后在需要的地方使用该变量。这种事情是可能的还是任何解决方法,以便可以避免多个if -else语句。

欢迎提出任何建议。 提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以尝试this.refresh存在并调用它。如果未定义,您可以回退到window.refresh。代码示例:

if(this.refresh === undefined) {
    window.refresh();
} else {
    this.refresh();
}

我不确定哪种方式最有意义,但我希望你能得到这个概念:如果某些东西是undefined,你就无法调用它,需要找出其他东西。

答案 1 :(得分:0)

扩展jsalonen回答

<script>
function browserRefresh()
{
   if(this.refresh === undefined) {
        window.refresh();
    } else {
        this.refresh();
    } 
}
</script>

当你想刷新时只需要打电话

browserRefresh();