Abdullah Ibrahem
Abdullah Ibrahem

Reputation: 21

Javascript external file

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>

Upvotes: 2

Views: 57

Answers (2)

JohnH
JohnH

Reputation: 2133

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>

Upvotes: 1

nikhil kumar
nikhil kumar

Reputation: 91

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;

}

Upvotes: 0

Related Questions