无法从第一个包含的JavaScript文件中调用第二个包含的js文件的函数 [英] Can't call functions from first included javascript file from the second included js file

查看:151
本文介绍了无法从第一个包含的JavaScript文件中调用第二个包含的js文件的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Cordova制作Android应用程式。一切都适用于Android 6.0,默认情况下使用Chrome,问题是在使用默认Android浏览器的Android 4.2.2(三星Galaxy S4 mini)。



如果我没有误解,那么在使用cordova编译并安装在Android操作系统上后,应用程序将在默认Android浏览器中启动。



Android浏览器的页面在加载时为空。但在Chrome中一切正常。



问题出在默认的Android 4.2.2浏览器。它不能在默认浏览器中使用诺基亚1520(使用Windows Phone操作系统)。



index.html:

 <!DOCTYPE html> 
< html>
< head>
< script src =js / jquery-3.1.1.min.jstype =text / javascript>< / script>
< script src =2.jstype =text / javascript>< / script>
< script src =1.jstype =text / javascript>< / script>
< / head>
< body>
< div id =content>
< / div>
< / body>
< / html>

1.js:

  $(document).ready(function(){
$('#content')。html(< span> test3< / span>); //我可以看到页面上的test3)。
showLogin();
});

2.js(这个文件里面没有内容,我看不到test1和test2 ):

  $('#content')。html(< span> test1< / span>); 

function showLogin(){
$('#content')。html(`< span>
test2
< / span>`)
}

我第一次尝试#1

我也尝试调用 setTimeout()内的 showLogin()

  setTimeout(function(){
showLogin();
},1000);

我第2次尝试



1.js:

  $(document).ready(function(){
$ ('#content')。html(< span> test3< / span>); //工作得很好
showLogin();
}

2.js(此文件内无效):

  $(document).ready(function(){
$('#content')。html(< span> test1< / span> );

function showLogin(){
$('#content')。html(`< span>
test2
< / span>`) ;
}
});


解决方案

问题是由多行JavaScript字符串`。



我改变了这个:

  showLogin(){
$('#content')。html(`< span>
test2
< / span>`)
}

到此:

  function showLogin(){
$('#content')。html(< span> test2< / span>);
}

一切都很好。


$ b b

看起来这些报价在股票Android浏览器中不受支持。我想这触发了2.js文件中的异常,这是2.js中的其他代码没有执行的原因。



我最终使用多线字符串的反斜杠如下:

  function showLogin(){
$('#content')。 html(< span> \
test2\
< / span>);
}

反斜杠也可以在单引号内,例如:

  function showLogin(){
$('#content')。html('< span> \
test2 \
< / span>');
}


I'm making an Android application with Apache Cordova. Everything works on Android 6.0, which is using Chrome by default, the problem is on Android 4.2.2 (Samsung Galaxy S4 mini) which is using the default Android browser.

If i'm not mistaken, then the application is "started" in the default Android browser after it's compiled with cordova and installed on the Android operating system.

In the default Android browser the page is empty on load. But in Chrome everything works fine.

The problem is in the default Android 4.2.2 browser. It's not working in the default browser for Nokia 1520 (which is using the Windows Phone OS).

index.html:

<!DOCTYPE html>
<html>
    <head>
        <script src="js/jquery-3.1.1.min.js" type="text/javascript"></script>
        <script src="2.js" type="text/javascript"></script>
        <script src="1.js" type="text/javascript"></script> 
    </head>
    <body>
        <div id="content">
        </div>
    </body>
</html>

1.js:

$(document).ready(function() {
    $('#content').html("<span>test3</span>"); // Works fine (i can see test3 on the page).
    showLogin();
});

2.js (nothing inside this file works, i can't see test1 nor test2 on the page):

$('#content').html("<span>test1</span>");

function showLogin() {
    $('#content').html(`<span>
                        test2
                        </span>`);
}

WHAT I TRIED #1

I also tried to call the showLogin() inside setTimeout():

setTimeout(function() {
    showLogin();
}, 1000);

WHAT I TRIED #2

1.js:

$(document).ready(function() {
    $('#content').html("<span>test3</span>"); // Works fine.
    showLogin();
});

2.js (nothing inside this file works):

$(document).ready(function() {
 $('#content').html("<span>test1</span>");

    function showLogin() {
        $('#content').html(`<span>
                    test2
                    </span>`);
    }
});

解决方案

The problem was caused by the quotes for the multiline JavaScript string "`".

After i changed this:

function showLogin() {
    $('#content').html(`<span>
                        test2
                        </span>`);
}

To this:

function showLogin() {
    $('#content').html("<span>test2</span>");
}

everything worked just fine.

It looks like that these quotes are not supported in the stock Android browser. I guess that this triggered an exception in the 2.js file and that was the reason that the other code in 2.js didn't execute.

I ended up using the backslashes for the multiline strings like this:

function showLogin() {
        $('#content').html("<span>\
                            test2\
                            </span>");
}

The backslash also works inside single quotes, like this:

function showLogin() {
        $('#content').html('<span>\
                            test2\
                            </span>');
}

这篇关于无法从第一个包含的JavaScript文件中调用第二个包含的js文件的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆