简体   繁体   中英

JavaScript including another one

I am trying to make a plugin for a radio where the javascript is including another 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.

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.

Did getScript work for you?

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.

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.

It only works when the Javascript is evaluated by the browser. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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