1

I'm currently trying to write a JavaScript function that will take a URL from a form once the user has entered it into a textbox and clicked a button to open in a new window that contains an iframe. Here's what I currently have:

<!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>

The main problem I'm currently having is determining how to pass the url variable into the document.write() method. It will work fine when I try a typical source URL in the iframe, but naturually that defeats the purpose since I want to use a user passed-in value. Any assistance is welcomed, I have no vanity-of-authorship so if I'm fundamentally doing things wrong, I'm ok with that -- seeking you pros to let me know.

1
  • 1
    I'm just wondering what's the point of the iframe if you already use a new window. Commented Oct 4, 2013 at 0:16

1 Answer 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");
Sign up to request clarification or add additional context in comments.

3 Comments

Lance, thanks a million! That worked perfectly and solved the issue. Is there some way I can mark this as solved? Again -- you rock!
Sure. Just click on the green checkmark next to my answer and upvote it too by clicking the up arrow! Glad that I was able to help!
I hit the check mark, but don't have high enough reputation yet for an upvote -- I'll come back when I do. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.