Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have this code working:

<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript"></script>
</head>
<body>
   <img id='qrcode' src=''>
   <button onclick="newQR()">Gerar QRcode</button>
   <script>  function newQR() {
   var x = Math.floor((Math.random() * 99999999999999999) + 1);
   document.getElementById('qrcode').src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + x + "&size=[500]x[500]&ecc=L&qzone=4&format=png";
 }
   </script>
    </body> 

Now I am trying to divide creating a js file and putting the script code inside. Right now my code is:

html file

<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript"></script>
</head>
<body>
  <img id='qrcode' src=''>
  <button onclick="newQR()">Gerar QRcode</button>
  <script type = "text/javascript" src = "js/script.js"></script>
</body>
</html>

javascript file (that it is inside a paste called js)

  function newQR() {
   var x = Math.floor((Math.random() * 99999999999999999) + 1);
   document.getElementById('qrcode').src = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=" + x + "&size=[500]x[500]&ecc=L&qzone=4&format=png";
 }

Why it is not working anymore?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
190 views
Welcome To Ask or Share your Answers For Others

1 Answer

I would assume it is because you link your javascript file after you have the function called. I would rather have the javascript reference in the head.

<!DOCTYPE html>
<html>
 <head>
  <script type = "text/javascript" src = "js/script.js"></script>
 </head>
 <body>
  <img id='qrcode' src=''>
  <button onclick="newQR()">Gerar QRcode</button>
 </body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...