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!

1

1 Answer 1

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

3 Comments

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 = " 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!
try this document.getElementById("my_url").src = "www." + {{x}} + ".com"
I was able to find a workaround by constructing the whole URL on the flask side and passing that to the html side. thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.