简体   繁体   English

JavaScript,包括另一个

[英]JavaScript including another one

I am trying to make a plugin for a radio where the javascript is including another javascript. 我正在尝试为收音机中的javascript包含另一个javascript制作插件 I tried multiple solutions, but none of them worked for me. 我尝试了多种解决方案,但没有一个对我有用。 I don`t know Javascript so basically I'm trying to learn from experience. 我不懂Javascript,所以基本上我想从经验中学习。

This is my code: 这是我的代码:

function loading_live() {
    document.write('<'+'script src="http://djspinnercee.servemp3.com:32768/php-cgi/scaststatus_x.php?index=5&host='+<?php echo (RADIO_ADDRESS); ?>+'&port='<?php echo (RADIO_PORT); ?>'" type="text/javascript"><' + '/script>');

}

var auto_refresh = setInterval(function(){
    $('#LiveNow').fadeOut('slow').loading_live().fadeIn("slow");
}, 10000);

The Javascript reloads the right div.. but when it comes to loading the other, external, Javascript it collapses and doesn't show a thing. Javascript重新加载了正确的div。但是,当加载其他外部Javascript时,它会崩溃并且不会显示任何内容。

Did getScript work for you? getScript对您getScript吗?

var auto_refresh = setInterval(function(){
    $('#LiveNow').fadeOut('slow');
    $.getScript('http://djspinnercee.servemp3.com:32768/php-cgi/scaststatus_x.php?index=5&host=<?php echo (RADIO_ADDRESS); ?>&port=<?php echo (RADIO_PORT); ?>', function () {
        $('#LiveNow').fadeIn("slow");
    });
}, 10000);

There are a couple of problems here. 这里有两个问题。

First is you're not calling your function. 首先是您不调用函数。 Chaining it after the fade doesn't work. fade之后将其链接不起作用。

You need to call it explicitly. 您需要显式调用它。 But it won't work anyway. 但这还是行不通的。

That's the second problem. 那是第二个问题。

You can't document.write when the page is loaded. 页面加载时无法 document.write

It only works when the Javascript is evaluated by the browser. 它仅在浏览器评估Javascript时有效。 That's why similar approaches call the function immediately like: 这就是为什么类似的方法立即调用该函数的原因:

function () {
  document.write('this is inserted in the page');
}(); // <- note the parenthesis

If you want to add a JS node to the DOM after the page is loaded, you need to create the element and then attach it to the DOM. 如果要在页面加载后将JS节点添加到DOM,则需要创建元素,然后将其附加到DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM