javascript - include an external JS file into HTML - Stack Overflow

We have an assignment where I need to insert an external JavaScript file in a html.HTML File<!DOCTY

We have an assignment where I need to insert an external JavaScript file in a html.

HTML File

 <!DOCTYPE html>
 <title>Color Guessing Game</title>
 <body onload="do_game()">
      <script type="text/javascript" src="/colguess.js"></script>
 </body>
 </html>

JS File

var guess_input_color;
var finished=false;
var colors=["blue","cyan","gold","gray","green","magenta","red","white","yellow"];
var guesses=0;
function do_game(){
  var n=Math.floor(Math.random()*9);
  var color=colors[n];
  while(!finished){
    guess_input_color=prompt("i am thinking of one of these colors:\n\n"+colors+"\n\nWhat color am i thinking of?");
    ++guesses;
    finished=check_guess();
  }
}
function check_guess(){
  if(colors.indexOf(guess_input_color)==-1)
   {
     alert("Sorry, i don't recognize your color.\n\nPlease try again.");
     return false;
   }
  if(color>guess_input_color){
        alert("Sorry, your guess is not correct!\n\nHint:your color is alphabetically lesser than mine\n\nPlease try again.");
         return false;
    }
   if(color<guess_input_color){
        alert("Sorry, your guess is not correct!\n\nHint:your color is alphabetically higher than mine\n\nPlease try again.");
        return false;
    }
    document.body.style.backgroundColor = guess_input_color;
    document.body.style.backgroundColor = guess_input_color;
    alert("Congratulations! You have guessed the color!\n\nIt took you"+guesses+" guesses to finish the game!\n\n")
    return true;
   }

Note: both html and js files are in the same directory.

We have an assignment where I need to insert an external JavaScript file in a html.

HTML File

 <!DOCTYPE html>
 <title>Color Guessing Game</title>
 <body onload="do_game()">
      <script type="text/javascript" src="/colguess.js"></script>
 </body>
 </html>

JS File

var guess_input_color;
var finished=false;
var colors=["blue","cyan","gold","gray","green","magenta","red","white","yellow"];
var guesses=0;
function do_game(){
  var n=Math.floor(Math.random()*9);
  var color=colors[n];
  while(!finished){
    guess_input_color=prompt("i am thinking of one of these colors:\n\n"+colors+"\n\nWhat color am i thinking of?");
    ++guesses;
    finished=check_guess();
  }
}
function check_guess(){
  if(colors.indexOf(guess_input_color)==-1)
   {
     alert("Sorry, i don't recognize your color.\n\nPlease try again.");
     return false;
   }
  if(color>guess_input_color){
        alert("Sorry, your guess is not correct!\n\nHint:your color is alphabetically lesser than mine\n\nPlease try again.");
         return false;
    }
   if(color<guess_input_color){
        alert("Sorry, your guess is not correct!\n\nHint:your color is alphabetically higher than mine\n\nPlease try again.");
        return false;
    }
    document.body.style.backgroundColor = guess_input_color;
    document.body.style.backgroundColor = guess_input_color;
    alert("Congratulations! You have guessed the color!\n\nIt took you"+guesses+" guesses to finish the game!\n\n")
    return true;
   }

Note: both html and js files are in the same directory.

Share Improve this question edited Sep 16, 2021 at 13:06 Nimantha 6,4836 gold badges31 silver badges76 bronze badges asked Aug 24, 2016 at 19:25 yashyash 1,3573 gold badges24 silver badges35 bronze badges 2
  • 2 Change /colguess.js to colguess.js. The / is saying "go to the root of the server, or drive, then find this file". – Mike Cluck Commented Aug 24, 2016 at 19:26
  • Thank you @MikeC. i got it now – yash Commented Aug 24, 2016 at 19:27
Add a ment  | 

1 Answer 1

Reset to default 5

IT should be this

<script type="text/javascript" src="colguess.js"></script>

not

<script type="text/javascript" src="/colguess.js"></script>

When you reffer '/', it tends to say, looking for the file into the root directory. if files are in the same folder as in html, then no need to use '/'.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742318318a4421290.html

相关推荐

  • javascript - include an external JS file into HTML - Stack Overflow

    We have an assignment where I need to insert an external JavaScript file in a html.HTML File<!DOCTY

    11小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信