0

I would like to pass a variable from which I can build a URL link and then would like to embed the link in html. I tried doing the following:

<div class="main">
  <h2>Test App</h2>
  <iframe id=my_url src="myFunction()" width="900" height="1300" frameborder="0" allowfullscreen></iframe>
  <script type="text/javascript">
    function myFunction() {
      document.getElementById("my_url ").src = "www.google.com";
    }
  </script>

I eventually want to be able to get a variable from a flask app and create a URL and set it equal to document.getElementById("my_url ").src. But in the meantime, when I run this code I get a "404 Page Not Found" error. What am I doing wrong in the above example?

Thanks!

user4500293
  • 621
  • 1
  • 7
  • 18

1 Answers1

0

What you are doing wrong is:

1- id=my_url > it should be id="my_url" with the qoutes or double quotes

2- src="myFunction()" > you are setting your src to myFunction(), this wont triger the function

3- getElementById("my_url ") > it should be getElementById("my_url") without the space

4- "www.google.com" > you need to set src to a valid link, you need to add the protocol

here is the code and good luck:

<div class="main">
        <h2>Test App</h2>
        <iframe id="my_url" width="900" height="1300" frameborder="0" allowfullscreen></iframe>
</div>
<script type="text/javascript">
        function myFunction() {
            document.getElementById("my_url").src = "https://www.google.com";
        }
        myFunction()
</script>
Marik Ishtar
  • 2,899
  • 1
  • 13
  • 27
  • I wanted to pass a variable from flask to the url to build the url. Let's say the variable is called x. I thought I could do document.getElementById("my_url").src = " https://www." + {x} + ".com" and have x = google, for example. But this fails. Did I miss something? Usually when I pass a variable I have to put it in {{x}} to have it display the html but I don't think that works here either. Thanks! – user4500293 Jun 23 '20 at 04:35
  • try this document.getElementById("my_url").src = "https://www." + {{x}} + ".com" – Marik Ishtar Jun 23 '20 at 04:38
  • 1
    I was able to find a workaround by constructing the whole URL on the flask side and passing that to the html side. thanks! – user4500293 Jun 23 '20 at 05:01