Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将javascript添加到iframe src属性_Javascript_Jquery_Iframe - Fatal编程技术网

将javascript添加到iframe src属性

将javascript添加到iframe src属性,javascript,jquery,iframe,Javascript,Jquery,Iframe,我有一个javascript函数,在成功调用jQueryAjax后被调用 function launchTool(user, pass, name, hash, port) { var iframe = jQuery('<iframe id="AppletFrame" src="http://www.mysite.com/applications/applet.html"></iframe>'); var $dialog = jQuery('&l

我有一个javascript函数,在成功调用jQueryAjax后被调用

 function launchTool(user, pass, name, hash, port) 
 {
     var iframe = jQuery('<iframe id="AppletFrame" src="http://www.mysite.com/applications/applet.html"></iframe>');
     var $dialog = jQuery('<div></div>');
     $dialog.append(iframe);
     $dialog.dialog({
         title: name,
         width: '425',
         height: '400',
         modal: true,
         close: function () {
             iframe.attr("src", "");
         }
     });
 }
函数启动工具(用户、过程、名称、哈希、端口)
{
var iframe=jQuery(“”);
var$dialog=jQuery(“”);
$dialog.append(iframe);
$dialog.dialog({
标题:姓名,
宽度:'425',
高度:'400',
莫代尔:是的,
关闭:函数(){
iframe.attr(“src”,即“);
}
});
}
这将打开iframe中的java小程序,iframe显示在jQuery对话框中。这非常有效,但现在我需要将值传递给java小程序。这正是我需要的

所以我试了一下:

function launchTool(user, pass, name, hash, port) 
 {

     var iframe = jQuery('<iframe id="AppletFrame" src="about:blank"></iframe>');

     var doc = document.getElementById('AppletFrame').contentWindow.document;
     doc.open();
     doc.write('<script>');
     doc.write('var attributes = {code:"com.test.MyApplet", width:400, height:375};');
     doc.write('var parameters = {jnlp_href: "http://'+window.location.host+'/applications/MyConfigApplet.jnlp", user: "'+user+'", pass: "'+pass+'", name: "'+name+'", hash: "'+hash+'", port: "'+port+'"};');
     doc.write('</script>');
     doc.write('deployJava.runApplet(attributes, parameters, "1.6");');
     doc.close();

      var $dialog = jQuery('<div></div>');
     $dialog.append(iframe);
     $dialog.dialog({
         title: name,
         width: '425',
         height: '400',
         modal: true,
         close: function () {
             iframe.attr("src", "");
         }
     });
 }
函数启动工具(用户、过程、名称、哈希、端口)
{
var iframe=jQuery(“”);
var doc=document.getElementById('AppletFrame').contentWindow.document;
doc.open();
文件写入(“”);
write('var attributes={code:'com.test.MyApplet',宽度:400,高度:375};');
doc.write('var parameters={jnlp_href:'http://'+window.location.host+'/applications/MyConfigApplet.jnlp',用户:'+user+',过程:'+pass+',名称:'+name+',哈希:'+hash+',端口:'+port+'};');
文件写入(“”);
doc.write('deployJava.runApplet(属性、参数,“1.6”);');
doc.close();
var$dialog=jQuery(“”);
$dialog.append(iframe);
$dialog.dialog({
标题:姓名,
宽度:'425',
高度:'400',
莫代尔:是的,
关闭:函数(){
iframe.attr(“src”,即“);
}
});
}
基本上,我已经尝试将工作示例中的
applet.html
的内容添加到代码中。我没有看到任何错误,唯一出现的是一个空的iframe。我想这实际上并不适用于src属性。我做错了什么

编辑: 根据@TheZuck的建议,我添加了deployJava.js,其中包括:

doc.write(“”)

现在至少我得到了一个错误:
TypeError:document.getElementById(…)为null


为什么?

我不知道为什么,但我所做的一切都不能让代码正常工作。我放弃了将内容写入src的尝试,决定编写一个查询字符串来将参数传递给小程序本身:

var iframe = jQuery('<iframe id="AppletFrame" width="435" height="410" frameborder="0" src="http://myurl.com/applications/applet.php?user='+user+'&pass='+pass+'&name='+name+'&hash='+hash+'&port='+port+'"></iframe>');
var-iframe=jQuery(“”);

这是可行的,但不确定这是否是最优雅的解决方案。

您是否尝试过将小程序直接包含到父页面而不是iframe中?你可能会得到关于哪里出错的更有用的反馈。你有没有试过让代码运行onDomReady而不是在包含时立即运行?@Kato我可以在父页面上加载applet.html,效果很好。即使是我问题的第一部分,我把它放在一个iframe中,然后在对话框中显示它也很好。你忘了添加:java.com/js/deployJava.js“>?@TheZuck不,我把它放在我的主页的标题中。它被加载了。iframe是一个单独的实体,它不足以加载到你的主页中,它也需要在那里。。。