如图:获取自己定义页面的变量
iframe内容:
输出iframe的window里没有自己要拿取的变量
如图:获取自己定义页面的变量
iframe内容:
输出iframe的window里没有自己要拿取的变量
因为获取变量时,iframe中的页面还没有加载完毕,你需要为iframe设置onload事件。在onload事件中获取变量
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
</head>
<body>
<iframe src="1.html" width="500" height="300" id="iframeid" onload="fun()"></iframe>
<script type="text/javascript">
function fun() {
var iframe = document.getElementById("iframeid").contentWindow;
console.log(iframe.cs);
}
</script>
</body>
</html>
1.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=9,chrome=1" />
<title> 页面名称 </title>
</head>
<body>
这是1.html
<script type="text/javascript">
var cs = "13456";
</script>
</body>
</html>