How to reload part of a web page using iframe instead of ajax?

The reason I don't want to use ajax for this is because the part I want to update is actually a compilation plugin implemented by another. I just posted the script fragment that they provided in my html code and it shows part of the comments in my articles. Since this is not a live comment, I want to add an refresh button next to it so that users can simply update the commenting part to get the latest comments without requiring a full page reload.

Therefore, I think iframe is an option for me. But the problem is that I need to specify the src iframe attribute. I don't know what value I should use, because all I have is just a fragment of a script. Can anyone give me any idea on this?

By the way, the code snippet looks like this:

<div id="uyan_frame"></div> <script type="text/javascript" id="UYScript" src="http://v1.uyan.cc/js/iframe.js?UYUserId=1674366" async=""></script> 
+4
source share
3 answers

You can put the snippet above in an html file like this

 <!DOCTYPE html> <html> <body> <div id="uyan_frame"></div> <script type="text/javascript" id="UYScript" src="http://v1.uyan.cc/js/iframe.js?UYUserId=1674366" async=""></script> </body> </html> 

And then use this file as src of your iFrame, which you can update with javascript.

However, just because you can do something does not mean that you have to do something. This is really a hacker way to do what you are trying to do. Several alternatives:

  • Understand what the script you use is doing and work with it. Judging by the name of the script and div in the fragment, this could be creating an iframe to begin with. If so, why not just figure out what the iFrame is calling using your browsers, debug it and update it manually or modify the script to do this?

  • Use the Live update platform . It may not be possible for you, I don’t know your limitations, but there are many wonderful frameworks that support current updates. For example, Disqus comes to mind. Other examples are facebook comments or you can insert a link to an external site, for example branch

  • Use Ajax . I don't understand a bit whether this is your script that you are writing, or a third-party script. If this is your script, use generally accepted methods to do this type of work, unless you have a really big reason for not too much. You will get better support from others, you will gain more generally accepted experience, and for the most part best practices will get this name for some reason. People use the "ajax" methods for live update pages because they are effective and useful. Frames have become much less common on the Internet because they are clumsy and make it difficult for different parts of the page to interact. Unless you have a great reason not to use normal practice, this is usually your best bet.

+2
source

Just create the html page with the script you were talking about, and use this file in the iframe src attribute.

+3
source

You can do it:

 var iframe = document.getElementById('your_frame_id'); iframe.src = iframe.src; 

set iframe src to value again, this will update the frame and work with cross-domain frames

0
source

All Articles