Solution 1 :

You can recover address bare url with window.location.href then parse it through URL object and extract GET searchParams.

To reuse code you should allow your function to admit arguments.

Then wait till DOM is loaded to allow it to read and write DOM element tags.

This snippet should do the trick.

function setBrowserFrameSource( getParameter = null ) {
  var browserFrame = document.getElementById("browser");
  var urlName = document.getElementById("urlName");
  if ( getParameter === null ) {
    browserFrame.src = document.getElementById("txtUrl").value;
  } else {
    browserFrame.src = getParameter;

window.addEventListener('load', function() {
  var url = new URL(window.location.href);
  var getParam = url.searchParams.get("txtUrl");
  if ( getParam !== null ) {
    setBrowserFrameSource( getParam );

Problem :

I would like to change the iframe src URL using a URL. For example, if I input this URL to the browser and submit, it should change the iframe’s src part to and press the button “Go”.

This is my code:

<!DOCTYPE html>
<script type="text/javascript">
    function setBrowserFrameSource(){
        var browserFrame = document.getElementById("browser");
        browserFrame.src= document.getElementById("txtUrl").value;
<div visibility="hidden">

<form method="post" target="browser">
<input id="txtUrl" style="width:82%;" placeholder="Put the website here" name="url" type="text" />
<input style="width:8%;" type="button" value="Go" onclick="setBrowserFrameSource(); return false;"/>
<iframe id="browser" name="browser" src="" style="height:100%; width:100%"></iframe>


Comment posted by selection menu for iframe src using javascript

Does this answer your question?

Comment posted by Rob

Note: the