1

I have a calculator that is inside of an iframe.

 <iframe id="tool_iframe" scrolling="no" height="750" srcdoc="
  <html>
    <head>
      <style>
         @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
body{
  height:100vh;
  background-color: #4158D0;
background-image: linear-gradient(43deg, #4158D0 0%, #C850C0 46%, #FFCC70 100%);
  font-family: 'Poppins', sans-serif;
  background-position:center center;
}
      .container {
        width: 520px;
        background-color: #ffffff20;
        margin: 120px auto;
      }
      table {
        width: 100%;
        border-color: transparent;
          font-family: 'Poppins', sans-serif;
                border-radius:20px;


      }
      td {
        width: 25%;
      }
      button {
        width: 100%;
        height: 90px;
        font-size: 32px;
        background-color: white;
        border: none;
        color:#273342;
      }
      #inputLabel {
        height:100px;
        font-size: 60px;
        vertical-align: bottom;
        text-align: right;
        background-color: #27334210;
        color:white;
        padding:0 10px 0 0;
          font-family: 'Poppins', sans-serif;
                border-top-radius:20px;


      }
      </style>
      <script>
         var inputLabel = document.getElementById('inputLabel');
      function pushBtn(obj) {
        var pushed = obj.innerHTML;
        if (pushed == '=') {
          //Calculate
          inputLabel.innerHTML = eval(inputLabel.innerHTML);
        } else if (pushed == 'AC') {
          //All clear
          inputLabel.innerHTML = '0';
        } else {
          if (inputLabel.innerHTML == '0') {
            inputLabel.innerHTML = pushed;
          } else {
            inputLabel.innerHTML += pushed;
          }
        }
      }
      </script>
    </head>
    <body>
       <html>
<body>
    <div class="container">
      <table border="1" cellspacing="0">
        <tr>
          <td colspan="4" id="inputLabel">0</td>
        </tr>
        <tr>
          <td colspan="3"><button onclick="pushBtn(this);">AC</button></td>
          <td><button onclick="pushBtn(this);">/</button></td>
        </tr>
        <tr>
          <td><button onclick="pushBtn(this);">7</button></td>
          <td><button onclick="pushBtn(this);">8</button></td>
          <td><button onclick="pushBtn(this);">9</button></td>
          <td><button onclick="pushBtn(this);">*</button></td>
        </tr>
        <tr>
          <td><button onclick="pushBtn(this);">4</button></td>
          <td><button onclick="pushBtn(this);">5</button></td>
          <td><button onclick="pushBtn(this);">6</button></td>
          <td><button onclick="pushBtn(this);">-</button></td>
        </tr>
        <tr>
          <td><button onclick="pushBtn(this);">1</button></td>
          <td><button onclick="pushBtn(this);">2</button></td>
          <td><button onclick="pushBtn(this);">3</button></td>
          <td><button onclick="pushBtn(this);">+</button></td>
        </tr>
        <tr>
          <td colspan="2"><button onclick="pushBtn(this);">0</button></td>
          <td><button onclick="pushBtn(this);">.</button></td>
          <td><button onclick="pushBtn(this);">=</button></td>
        </tr>
      </table>
    </div>
</body>
</html>
    </body>
  </html> "></iframe>

The actual website is https://tropical.team/tool/calculator/ if you want to take a look for yourself. You can see when you load in everything loads fine but the calculator just doesn't work, i know the code is valid because when i copy the script from the head of the iframe and paste it into the consxole it workks. This must mean that it's a problem with the document.reference not working inside the iframe. The calculkator code is user made which means i can only modify it automaticaly in minor ways, like changing all document references to something else. What could i chage them to that would generally fix this?

treepek
  • 37
  • 6
  • The quoting issue has been fixed, that was a typo, in the actual code, all double quotes have been escaped. If you go to the link you should be able to see in the developer tools, that all those elements are inside the iframe's body that are getting referenced – treepek Jan 04 '23 at 11:45
  • The quoting is still off in the example. The code is awfull, never use inline listeners, and never attach a listener to every cell of the table. Please check [how to delegate events](https://stackoverflow.com/q/1687296/1169519). The error is fired because of the timing, you're reading `inputLabel` in the head section, at that time the element is not yet created. – Teemu Jan 04 '23 at 15:28
  • @Teemu thanks for trading my code, but what about give me a solution? Should i maybe wrap the js in an window.on("load") or what – treepek Jan 04 '23 at 20:36
  • That, or move it at the end of the body. – Teemu Jan 05 '23 at 04:54
  • I'm sorry, if the comment above feels a bit harsh. It's not your fault, it's the tutorials you're following. No matter how good they were at their time, they are now blatantly outdated. Find something written after 2015, make sure the JS book covers ES6 or later (the current version is 13). [MDN](https://developer.mozilla.org/en-US/) is a great source of [JS documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript) and contains plenty of [good tutorials](https://developer.mozilla.org/en-US/docs/Learn). Practically there's everything you need for web developement. – Teemu Jan 05 '23 at 05:49
  • Thanks for clarifying. I will definitely look out for the dates, very appreciated. – treepek Jan 05 '23 at 17:05
  • @Teemu if you would like, you could post that as an answer – treepek Jan 05 '23 at 17:06

0 Answers0