javascript - 在 iframe 中传递 javascript 变量
问题描述
我正在尝试在 iframe 中传递 javascript 变量
这是第一个域中的 index.html
<script>var details ='test999'</script>;
<iframe src="http://somedomain.com/99.html" frameborder="0" width="100%" ;scrolling="yes""></iframe>
在其他域中,我正在使用以下代码上传 99.html
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#prithere").html('' + details + '');
});
</script>
<div id="prithere"></div>
我没有收到跨源问题我收到了这个未定义的错误。Uncaught ReferenceError: details is not defined 这是因为代码是在加载 iframe 之前执行的吗?如果是这样,有什么解决办法吗?我不能使用 onclick
解决方案
如果可以的话,它parent.details
只是将它传递给页面:
<script>
var details ='test999';
document.write('<iframe src="http://somedomain.com/99.html#details='+details+'"....</iframe>');
</script>
然后
$(document).ready(function() {
$("#prithere").html(location.hash.split("details=")[1]);
});
推荐阅读
- go - Go 尝试处理错误而不是我
- java - 如何使我的字符串成为可以重定向到另一个网站的超链接
- java - Maven Wagon ssl 不安全标志的 Gradle 等效项
- winforms - WebView2 不能在缩放的监视器上缩放
- c# - 西装外套 | MDE | 在应用程序中随时获取功能键的值
- python - Leetcode 编译器在为具有多个方法的类执行输入时如何工作?
- windows - PowerShell 多条语句单行搞乱了标准输出顺序
- php - 如何使用 PHP 从服务器上的保管箱保存文件
- java - 如何声明一个接受任何类型 Function<> 的方法引用的变量
- etl - PENTAHO SPOON - 将单个表格输入拆分为多个 excel 输出