1

我目前正在尝试编写一个 JavaScript 函数,一旦用户将其输入文本框并单击按钮以在包含 iframe 的新窗口中打开,它将从表单中获取 URL。这是我目前拥有的:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
label {
display:block;
}
</style>
<script type="text/javascript">

function goTo() {
    var url = document.forms[0].url.value;
    myWindow = window.open('', '', 'width=800,height=800');
    myWindow.document.write("<iframe src='url'></iframe>");
    myWindow.focus();
    return false;
}

</script>
</head>
<body>
<form action="" method="get" onsubmit="return goTo()">
    <label for="url">Enter the URL:
    <input type="text" name="url" id="url">
    <input type="submit" value="Submit">
</form>
</body>
</html>

我目前遇到的主要问题是确定如何将 url 变量传递给 document.write() 方法。当我在 iframe 中尝试典型的源 URL 时,它会正常工作,但由于我想使用用户传入的值,这自然会破坏目的。欢迎任何帮助,我没有自负的作者身份,所以如果我从根本上做错了,我可以接受——寻求专业人士让我知道。

4

1 回答 1

1

The url variable is part of a string in your example. Try this instead. Also, make sure that it's a valid URL.

myWindow.document.write("<iframe src='"+ url +"'></iframe>");

I might also add that writing an iframe in the new window is a bit redundant. you should just do

myWindow = window.open(url, "", "width=800,height=800");
于 2013-10-04T00:15:16.867 回答