2

This in my code for a simple task; Just switching the img src. It worked fine when the script was within the HTML page, but when I tried to make it an external file it did not work.

Any advice?

This is the external JS:

var link = document.getElementById('btn1');

link.onclick = function switch1() {
  var a = document.getElementById('img1');
  var b = document.getElementById('img2');

  var c = a.src;
  var d = b.src;
  document.getElementById('img1').src = d;
  document.getElementById('img2').src = c;
}

And this is the HTML file:

<!DOCTYPE html>
<html>

<head>
  <title></title>
  <meta charset="utf-8" />
  <script src="JavaScript.js"></script>

  <style>
    .box {
      position: absolute;
      display: block;
      background-color: wheat;
      width: 90%;
      height: 60%;
    }
    .frame {
      position: relative;
      display: block;
      background-color: Window;
      left: 10%;
      top: 10%;
      width: 80%;
      height: 80%;
    }
    .content {
      position: relative;
      display: block;
      background-color: yellow;
      left: 10%;
      top: 10%;
      width: 80%;
      height: 80%;
    }
    .img1 img {
      position: absolute;
      display: block;
      left: 10%;
      top: 15%;
      width: 150px;
      height: 150px;
    }
    .img2 img {
      position: absolute;
      display: block;
      right: 10%;
      top: 15%;
      width: 150px;
      height: 150px;
    }
    .button {
      position: absolute;
      display: block;
      background-color: #4CAF50;
      border: none;
      color: white;
      padding: 15px 32px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin: 4px 2px;
      cursor: pointer;
      top: 15%;
      right: 42%;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="frame">
      <div class="content">
        <div class="img1">
          <img src="pic/land2.jpg" id="img1" alt="img1">
        </div>
        <div class="img2">
          <img src="pic/land1.jpg" id="img2" alt="img2">
        </div>
        <div class="btn">
          <button class="button" id="btn1">Button</button>
        </div>
      </div>
    </div>
  </div>
</body>
</html>
2
  • 2
    put script tag after your HTML Commented Feb 13, 2017 at 12:33
  • or wrap in window.onload=function() { ... } Commented Feb 13, 2017 at 12:34

2 Answers 2

1

If you are using HTML5, use a forward slash character before the script file name if the file resides in the website's root directory.

<script src="/JavaScript.js"></script>

If you are using HTML4 or XHTML, also add the script type.

<script src="/JavaScript.js" type="text/javascript"></script>
Sign up to request clarification or add additional context in comments.

Comments

0

you have to do one thing to make it work,

write your javascript code/function inside this block in external file:

window.onload= function(){
   var link= document.getElementById('btn1');

    link.onclick = function switch1() {
    var a = document.getElementById('img1');
    var b = document.getElementById('img2');

    var c = a.src;
    var d = b.src;
    document.getElementById('img1').src = d;
    document.getElementById('img2').src = c;

}

1 Comment

Yup. Just like I commented - will however only work if that is the only .onload in the page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.