开发者

refreshing javascript by renaming src attribute

开发者 https://www.devze.com 2023-02-03 01:27 出处:网络
I want to refresh the output of the script below.Is this json? Do I need to add a crossdomain policy in my site?

I want to refresh the output of the script below. Is this json? Do I need to add a crossdomain policy in my site?

<div id="nowplaying">
   <script src="http://s4.total-streaming.com/xml.php?station=1269&get=js"></script>
</div>

Edit:

This is what I'm trying based on @alexn advise, but still doesn't refresh.

<div id="nowplaying">
    <script id="nowplaying-script" src="http://s4.total-streaming.com/xml.php?station=1269&get=js"></script>

    <script>
        setInterval(funct开发者_如何学JAVAion () {
            $('#nowplaying-script').attr('src', 'http://s4.total-streaming.com/xml.php?station=1269&get=js');
        }, 1000);
    </script>
</div>

Note Firebug: Resource interpreted as script but transferred with MIME type text/html. xml.php:-1


No, it's plain JavaScript. The script will simply output a string. You don't need any cross-domain policies to use this.

To refresh the content, just re-assign the src attribute of your script tag to the same url. You can use setTimeout to do this on a specified interval. Something like this should do it, you need to ad an id attribute to your script tag. The following will refresh every 5:th second.

setInterval(function() {
    var element = document.getElementById("nowplaying-script");
    element.setAttribute("src", "http://s4.total-streaming.com/xml.php?station=1269&get=js");
}, 5000);

(This is untested)


I would lean towards this being a cache problem.

How about you try this plugin (or look into how it works): http://plugins.jquery.com/project/reload

It essentially would allow you to do this:

setInterval(function () {
  $('#nowplaying-script').reload();
}, 1000);

If you don't want to use the plugin, all it's doing is appending the current date to the URI params.

Another thing I noticed is that the script you are loading contains document.write. Even if you managed to get it to reload, it's not going to do what your expecting it to do.

In fact, calling document.write on a already loaded HTML page will cause your page to turn blank and contain only the content passed to document.write.

Now I'm not sure if your script is running on the same hostname as the script you are loading, if isn't you going to need a JSONP API instead of an a script that writes into the page.

If they do not offer a JSONP API, a more hackish way to solve this problem would be to write a server-side script that acts as a proxy. It essentially would load http://s4.total-streaming.com/xml.php?station=1269&get=js, parse it server-side using a substr or RegEx and return back the text that you want, in this case the name of the song currently playing.

Another way to solve this problem would be to overwrite the document.write method and have it print the content into the div instead of onto the screen like this:

document.write = function(songName) {
  $('#nowplaying').text(songName);
};

Personally I wouldn't use this solution, but it will work if the other solutions are too complex for you to setup. Keep in mind all calls to document.write on that given page will print the content into your #nowplaying element. You should probably move your SCRIPT tag out of the #nowplaying element too if you use the above solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消