添加javascript到iframe src属性
Add javascript to iframe src attribute
我有一个javascript函数,在jQuery AJAX调用成功后被调用。
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", "");
}
});
}
这将打开一个java applet,它在iframe中显示在jQuery对话框中。这工作得很好,但现在我需要将值传递给java applet。这个post看起来正是我需要的。
所以我试了这个:
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", "");
}
});
}
基本上,我试图在我的工作示例中添加applet.html
的内容到代码。我没有看到任何错误,唯一出现的是一个空iframe。我猜这实际上并不适用于src属性。我做错了什么?
编辑:根据@TheZuck的建议,我添加了deployJava.js:
doc.write('<script src="https://www.java.com/js/deployJava.js"></script>')
现在至少我得到一个错误:TypeError: document.getElementById(...) is null
为什么?
我不知道为什么,但我所做的一切都会让代码工作。我已经放弃了将内容写入src的尝试,并决定编写一个查询字符串来将参数传递给applet本身:
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>');
这可以工作,但不确定这是否是最优雅的解决方案。
相关文章:
- 从其他元素上的单击事件访问image src属性
- 使用json数据更改视频标签中的src属性
- 为什么html src属性中的相对url是因为Javascript函数中的完整路径
- 无法在
标记 SRC 属性中传递参数
- 单击没有 src 属性的 iframe 中的元素
- img src 属性在为空时返回页面的 URL
- 如何从浏览器隐藏视频src属性's检查元素
- 为什么当我尝试为iframe-src属性构建URL时,AngularJS会抛出错误
- Windows Phone浏览器控件执行JS函数来替换src属性
- 使用jQuery通过更改src属性来设置一个图像标签的动画
- 使用angularjs中的自定义服务交换ng src属性
- 使用angularjs交换图像标签中的ng-src属性
- AngularJS - 如何在 iframe 元素更改时读取 SRC 属性
- 浏览器检测和 Firefox data-src 属性
- 延迟加载引导传送带而不更改 img src 属性
- 根据 JSON 更改图像的 src 属性
- 如何在 ajax 请求的回调中设置 ngInclude 元素的 src 属性
- 如何制作 AngularJS 指令来更改图像的 src 属性
使用 Jquery 不会更改 src 属性
- 修改其<源>元素的 src 属性后,视频不会更新