简体   繁体   English

如何使用iframe而不是ajax重新加载部分网页?

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

The reason I don't want to use ajax to do this is because the part I want to refresh is actually a commenting plugin implemented by other. 我不想使用ajax这样做的原因是因为我要刷新的部分实际上是由其他人实现的注释插件。 I just put a snippet of script they provided in my html code and it shows a commenting part under my articles. 我只是在他的html代码中放了一段他们提供的脚本,它在我的文章下面显示了一个评论部分。 As it's not a live commenting one, I want to add a refresh button next to it to enable users to just refresh the commenting part to get the latest comments without need for reloading the whole page. 由于它不是实时评论,我想在它旁边添加一个刷新按钮,使用户只需刷新评论部分即可获得最新评论,而无需重新加载整个页面。

Therefore, I think maybe iframe is an option for me. 因此,我认为iframe可能是我的选择。 But the problem is I need to specify the src attribute of iframe. 但问题是我需要指定iframe的src属性。 I don't know what value I should use because all I have is just a snippet of script. 我不知道我应该使用什么价值,因为我所拥有的仅仅是一段脚本。 Can someone give me any idea on this? 有人能给我任何想法吗?

By the way, the code snippet is as follows: 顺便说一句,代码片段如下:

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

只需使用您在里面讨论的脚本创建一个html页面,并在iframe src属性中使用此文件。

You can put the snippet above in an html file like this 您可以将上面的代码段放在像这样的html文件中

<!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 that file as the src of your iFrame, which you can refresh using javascript. 然后使用该文件作为iFrame的src,您可以使用javascript刷新它。

That said, just because you can do something doesn't mean you should do something. 也就是说,仅仅因为你可以做某事并不意味着你应该做点什么。 This is a really hacky way of doing what you're trying to do. 这是一种真正的hacky方式来做你想做的事情。 A few alternative options: 一些替代选择:

  1. Understand what the script you're using is doing, and work with it. 了解您正在使用的脚本正在做什么,并使用它。 Judging by the name of the script and div in the snippet, it may be creating an iframe to begin with. 根据代码段中脚本和div的名称来判断,它可能会创建一个iframe。 If thats the case, why not just figure out what that iFrame is called using your browsers debug it and refresh it manually, or modify the script to do so? 如果是这样,为什么不弄清楚使用浏览器调用它并手动刷新iFrame是什么,或者修改脚本来执行此操作?

  2. Use a live updating framework - This may not be possible for you, I don't know your constraints, but there are plenty of great commenting frameworks out there that do live updates. 使用实时更新框架 - 这对您来说可能是不可能的,我不知道您的约束,但是有很多很棒的评论框架可以进行实时更新。 For instance Disqus comes to mind. 例如,想到了Disqus Other examples are facebook comments or you could embed a reference to an external site like branch 其他示例是Facebook评论,或者您可以嵌入对分支等外部网站的引用

  3. Use Ajax - I'm a bit unclear on whether this is your script that you're writing, or a 3rd party script. 使用Ajax - 我有点不清楚这是你正在编写的脚本还是第三方脚本。 If it is your script, then use the generally accepted methods for doing this type of work, unless you have a really great reason not too. 如果它是你的脚本,那么使用普遍接受的方法来完成这类工作,除非你有一个非常好的理由。 You'll get better support from others, you'll gain more generally applicable experience, and for the most part, best practices gain that name for a reason. 您将获得其他人更好的支持,您将获得更普遍适用的经验,并且在大多数情况下,最佳实践获得该名称是有原因的。 People use "ajax" methods for live updating pages because its effective and useful. 人们使用“ajax”方法进行实时更新页面,因为它有效且有用。 Frames have become much less common on the web because they're clunky and make it difficult for different parts of the page to interact. 框架在网络上变得越来越不常见,因为它们很笨拙并且使页面的不同部分难以交互。 If you don't have a great reason not to use the common practice, its usually your best bet. 如果你没有充分的理由不使用常规练习,通常是你最好的选择。

You could do this : 你可以这样做:

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

set the iframe src to its value again, this will cause the frame to refresh, and will work with cross domain frames 再次将iframe src设置为其值,这将导致帧刷新,并将与跨域帧一起使用

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

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