简体   繁体   English

用jQuery刷新div的内容

[英]Refresh content of a div with jquery

I know this answer has been repeated, but no one worked for me, I don't know why :(. I have one div tag, which contains a script tag (from a website) and loads a kind of frame. That frame displays a monitor thanks to several variables, like the id (which is the most important). So, I want every 5 seconds, the id changes to display another monitor. 我知道这个答案已经重复了,但是没有人为我工作,我不知道为什么:(。我有一个div标签,其中包含一个脚本标签(来自网站)并加载一种框架。该框架显示一个监视器要归功于多个变量,例如id(最重要),所以,我希望每5秒更改一次id,以显示另一个监视器。

The problem is that in my refresh code I use the method load(), but in Firefox I get this warning "A call to document.write() from an asynchronously-loaded external script was ignored." 问题是,在刷新代码中,我使用方法load(),但是在Firefox中,我收到此警告:“从异步加载的外部脚本对document.write()的调用被忽略。” and in chromium I get this one "Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened." 在Chrome中,我得到了一个“无法在'Document'上执行'write'的操作:除非已明确打开,否则无法从异步加载的外部脚本写入文档”。

This is my code: 这是我的代码:

<script type="text/javascript">
    var id = ["id1", "id2", "id3"];        
        var number;
        var i = 0;
        function change(){

            if(i<id.length){
                number = id[i];
                i++;
            }else if(i>=id.length){
                clearInterval();
                number = id[i];
                console.log("start again");
            }                   
            return number;
        }

        function refresh(){
            $('#midStag').load("myUrl" +  ' #midStag');
        }

   setInterval(refresh, 5000);
</script>

<div id="midStag">
<script type="text/javascript">
    //I only put the id because is the more important variable 
    //and the only one that I am changing
    id = cambio();
    /*more variables like width or height*/

</script>
<script type="text/javascript" src="http://dashboard.monitis.com/sharedModule/shareModule.js"></script>

I really need it, please. 我真的需要它。 Any help I would appreciate that. 任何帮助,我将不胜感激。 If you don't understand something just tell it me and I'll correct. 如果您听不懂,请告诉我,我会纠正。 Thank you. 谢谢。

This is because the shareModule.js script has a document.write statement at the end. 这是因为shareModule.js脚本的shareModule.js有一个document.write语句。 And since you are reloading that script when you use .load() you get the error since the document has been closed and the script is being loaded asynchronously. 并且由于在使用.load()时正在重新加载该脚本,因此会收到错误消息,因为文档已关闭并且脚本正在异步加载。

You could do a few things to fix this: 您可以做一些事情来解决这个问题:

  1. Rewrite the script to not use document.write , but that requires access to the server it is stored on 重写脚本以不使用document.write ,但这需要访问存储在该服务器上的服务器
  2. Save the script locally and modify it to not use document.write . 将脚本保存在本地并将其修改为不使用document.write The only con would be you would have to update the script manually whenever sharedModule.js is updated on the server. 唯一的sharedModule.js是,每当服务器上更新sharedModule.js时,您都必须手动更新脚本。
  3. Edit the frame's url replacing the needed information, ie replace your new id with the old one. 编辑框架的URL替换所需的信息,即用旧ID替换新ID。 Then set the src with the new url. 然后使用新的URL设置src。

     var src = $("span iframe").prop("src"); src = src.replace(/publicKey=\\d+/,"publicKey="+newKey); $("span iframe").prop("src",src); 
  4. Replace document.write with a function that will insert html in a more friendly manner 用将以更友好的方式插入html的函数替换document.write

     document.write = function(markup){ document.currentScript.parentElement.insertAdjacentHTML('beforeend',markup); }; 

Demo replacing document.write 演示替换document.write

 monitis_embed_module_width = 100; monitis_embed_module_height = 100; monitis_embed_module_id = 300; document.write = function(markup){ document.currentScript.parentElement.insertAdjacentHTML('beforeend',markup); }; var div = document.getElementById("test"); setTimeout(function(){ var s = document.createElement("script"); s.src = "http://dashboard.monitis.com/sharedModule/shareModule.js"; div.innerHTML = ""; div.appendChild(s); },1000); 
 <div id="test"> Simulating load... </div> 

Demo replacing src 演示替换src

 var div = document.getElementById("test"); var frame = null; function reloadFrame(){ frame = frame || div.querySelector("iframe"); var src = frame.src; var id = Date.now(); frame.src = src.replace(/publicKey=\\d+/,"publicKey="+id); } setTimeout(reloadFrame,2000); 
 <div id="test"> <script> monitis_embed_module_width = 100; monitis_embed_module_height = 100; monitis_embed_module_id = 300; </script> <script src="http://dashboard.monitis.com/sharedModule/shareModule.js"></script> </div> 

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

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