简体   繁体   English

js 文件使用 html 页面下载,但 js function 仅在刷新页面后才有效

[英]js file downloads with html page but js function works only after refreshing the page

parent.html: OnClick="popup();"

1. page opens and displays contents 1.页面打开并显示内容
2. but js function isn't working. 2.但是js function 不工作。 js function is defined in js file. js function 在 js 文件中定义。
3. i checked using firebug, js file loded successfully. 3.我用firebug检查,js文件加载成功。
4. it only works after refreshing the page. 4.刷新页面后才有效。 Any idea please?请问有什么想法吗? what am i making mistake?我在犯什么错误?
js function: js function:

<script>
    function popup(){
     var test = "<html><head>";
         test += "<script src= " + '"'+"jqueryTestfile.js"+'"'+ "> <" + '/' + "script>";
         test += "</head><body>";
         test += "<div id=" + '"' +"anyId" + '"' + ">";
         test += "</div>";
         test += "</body></html>"; 
     win.document.write(test);    
    }
    </script>

child pop-up and source code looks like:子弹出窗口和源代码如下所示:

<html> <head> 
   <script src = "jQueryTestFile.js"> </script>
 </head> <body> <div id = "anyId">
    some content here ...
 </div> </body> </html>

I resolved my problem after a no.我在拒绝后解决了我的问题。 of attempt and would like to share:尝试并想分享:

<script type="text/javascript">
    window.document.close();
</script>

Why were not js functions working even after downloading the files: because of i was opening (pop up) a new window using window.document.write() and this method was causing the problem so i used window.document.close() to force a page to 'finish loading'.为什么即使在下载文件后 js 功能也不能工作:因为我正在使用window.document.write()打开(弹出)一个新的 window 并且这种方法导致了问题,所以我使用window.document.close()强制页面“完成加载”。

var h = document.getElementsByTagName('head')[0],
    s = document.createElement('script');

s.type = 'text/javascript';
s.onload = function () { document.getElementById('hello').innerText = h.innerText; };
s.src = 'http://html5shiv.googlecode.com/svn/trunk/html5.js';
h.appendChild(s);

see: http://jsbin.com/uhoger见: http://jsbin.com/uhoger

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

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