好吧,我有点问两个问题,但回答任何一个部分都会帮助我。我将直接提出问题并直接跳到我需要帮助的地方。我正在尝试使用Javascript函数来定义iframe的某些元素,以便用户可以决定源,高度和宽度。我现在的代码如下所示。
<script language="javascript">
window.onload = webPage;
function webPage(){
var page = prompt("please enter the website","www.google.com");
var height = prompt("Please enter the height of the frame","800");
var width = prompt("Please enter the width of the frame","600");
}
</script>
<iframe src=page height=height width=width>
</iframe>
有没有办法可以重新加载iFrame,让它使用用户输入作为当前代码的元素?我需要做些什么才能将变量用于iFrame?感谢任何关注我的问题并期待您的回答的人。
答案 0 :(得分:0)
<script language="javascript">
window.onload = webPage;
function webPage(){
var page = prompt("please enter the website","www.google.com");
var height = prompt("Please enter the height of the frame","800");
var width = prompt("Please enter the width of the frame","600");
//new content here
document.getElementById("theIframe").innerHTML="<iframe src=\""+page+"\" height=\""+height+"\" width=\""+width+"\"></iframe>";
}
</script>
<div id="theIframe"></div>
答案 1 :(得分:0)
首先使用空白值和一个唯一的 ID 初始化IFrame,然后附加用户输入。
这是工作JSFiddle Link
动态更改值
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
</head>
<body>
<iframe id="icereaper666" src="" height="0" width="0"></iframe>
<script language="javascript">
window.onload = webPage;
function webPage(){
var page = prompt("please enter the website","http://www.w3schools.com/");
var height = prompt("Please enter the height of the frame","400");
var width = prompt("Please enter the width of the frame","400");
var iframeElement = document.getElementById("icereaper666");
iframeElement.src = page;
iframeElement.height = height;
iframeElement.width = width;
}
</script>
</body>
</html>
注意:您还可以使用 createElement 动态创建IFrame元素。 Check this out
使用创建元素:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
</head>
<body>
<script language="javascript">
window.onload = webPage;
function webPage(){
var page = prompt("please enter the website","http://www.w3schools.com/");
var height = prompt("Please enter the height of the frame","400");
var width = prompt("Please enter the width of the frame","400");
var iframeElement = document.createElement("iframe");
iframeElement.src = page;
iframeElement.height = height;
iframeElement.width = width;
document.body.appendChild(iframeElement);
}
</script>
</body>
</html>