简体   繁体   中英

force refresh rss feed with javascript

I am completely new to javascript and would like to show and refresh RSS feed in a web page. I managed to show it in the page but I have problem refreshing the feed.

I tried the suggestions from this forum using setinterval and settimeout together with head.appendChild but did not work. They do not refresh the page.

I also tried "location.reload" function, which refreshes the page but shows blank page after the refresh. The same with metadata refresh in HTML.

I will be really grateful if someone can suggest a solution. My latest code is below which shows the feed in the page but does not refresh.

Edit: I may also need to add that the page I am showing the results are inside a PowerBI dasboard (web content tile). It is similar to w3schools trial windows. I could not refresh the results in any of them.

Thanks,

   <!doctype html>
   <html>
   <head>

   </head>

   <body>


   <script type = "text/JavaScript">
    

     function load_js()
     {
        var versionUpdate = (new Date()).getTime(); 
        var head= document.getElementsByTagName('head')[0];
        var script= document.createElement('script');
        script.type = "text/javascript";
        script.src= '//rss.bloople.net/?url=http%3A%2F%2Frss.cnn.com%2Frss%2Fcnn_latest.rss&detail=-1&limit=10&showtitle=false&type=js'+ 
     versionUpdate;
        head.appendChild(script);
        script.remove;
     }
     load_js();

    setInterval(function(){ 
    
      load_js();
    
   }, 3000);


   </script>

   </body>
   </html>

Since you're creating the <script> tag on every iteration, you won't be able to update te source.

Consider remembering the <script> to update the src;

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>

        <script type = "text/JavaScript">

            this.tag = document.createElement('script');

            function load_js() {

                console.log('load_js()');

                var versionUpdate = (new Date()).getTime(); 
                var head= document.getElementsByTagName('head')[0];

                this.tag.type = "text/javascript";
                this.tag.src= '//rss.bloople.net/?url=http%3A%2F%2Frss.cnn.com%2Frss%2Fcnn_latest.rss&detail=-1&limit=10&showtitle=false&type=js'+ versionUpdate;
                
                head.appendChild(this.tag);
                this.tag.remove;
            }
            
            load_js();

            setInterval(function(){ 
                load_js();
            }, 3000);
        </script>
    </body>
</html>

I've tested this in a w3schools trial windows and the versionUpdate is updated on each iteration.

JsBIN Demo

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