473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

my javascript works only in IE9 but does not work in anyother browser

1
i have javascript code for a snake game but its only works with IE9 does anyone know how i can fix it?
here is the code


Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
  4.  <head>
  5.  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6.     <title>Snake Game</title>
  7.  <style type="text/css">
  8.         table#board {
  9.             margin: 0 auto;
  10.             border: 4px solid blue;
  11.             border-collapse:collapse;
  12.         }
  13.         table#board1 {
  14.             margin: 0 auto;
  15.             border: 4px solid blue;
  16.             border-collapse:collapse;
  17.         }
  18.         table#board1 td {
  19.             height: 40px;
  20.             width: 360px;
  21.             padding: 0;
  22.             margin: 0;
  23.             }
  24.  
  25.             .item{}
  26. .worm1 {
  27.   font-weight:bold;
  28.   font-size:12pt;
  29.   color:blue;
  30.   background:white;
  31.   width:150px;
  32.   BORDER-BOTTOM: #ffffff solid 0px; 
  33.   BORDER-TOP: #ffffff solid 0px; 
  34.   BORDER-LEFT: #ffffff solid 0px; 
  35.   BORDER-RIGHT: #ffffff solid 0px; 
  36. }
  37. .worm2 {
  38.   font-weight:bold;
  39.   font-size:12pt;
  40.   color:red;
  41.   background:white;
  42.   width:150px;
  43.   BORDER-BOTTOM: #ffffff solid 0px; 
  44.   BORDER-TOP: #ffffff solid 0px; 
  45.   BORDER-LEFT: #ffffff solid 0px; 
  46.   BORDER-RIGHT: #ffffff solid 0px; 
  47. }
  48. .info {
  49.   font-weight:bold;
  50.   font-size:10pt;
  51.   color:black;
  52.   background:white;
  53.   width:200px;
  54.   height:150px;
  55.   BORDER-BOTTOM: #ffffff solid 1px; 
  56.   BORDER-TOP: #ffffff solid 1px; 
  57.   BORDER-LEFT: #ffffff solid 1px; 
  58.   BORDER-RIGHT: #ffffff solid 1px; 
  59. }
  60. h1{
  61. text-align:center; 
  62. color: red; 
  63. }
  64. </style>
  65.  </head>
  66. <body background="background.jpg"> 
  67.  
  68. <h1 align="center">Snake Game!</h1>
  69.  
  70.  
  71.  
  72.  <script type="text/JavaScript">
  73.  
  74. //<![CDATA[
  75.  
  76.   var height = 20; /*number of boxes vertically*/ 
  77.   var width = 20; /*number of boxes horizontally*/
  78.   var speed = 100;    
  79.   width += 4;
  80.   var a = 0;
  81.   var b = 0;
  82. document.write("<table id=board bgcolor=black bordercolor=blue");
  83. document.write("align=center border=1 cellpadding=0 cellspacing=0><tr><td>");
  84. for (b = 0; b < height+2; b++) {
  85. document.write("<img src=end.gif width=0 height=0>");
  86. for (a = 0; a < width- 2; a++) {
  87. if ((b == 0) || (b == height+1)) {
  88. document.write("<img src=end.gif width=0 height=0>");/* the end.gif image is the inner side of the border*/
  89. }
  90. else 
  91. {
  92. document.write("<img src=blank.gif width=15 height=15>");/* blank image covers each box in the field area*/
  93. }
  94. }
  95. document.write("<img src=end.gif width=0 height=0><br>");
  96. }
  97. document.write("</td></table>");
  98. /*second cell*/
  99. document.write("<table id = board1>");
  100. document.write("<td valign=top align=center bgcolor=#00CED1>");/*we have used valign to insure the <p> tag stays on top and other text follows the vertical alignment*/
  101. document.write("<p align=center><strong><font color=blue>Points score </font></strong></p>");
  102. /* point score area*/
  103. document.write("<br>");
  104. document.write("<form name=info>");
  105. document.write("<input type=button size=30 value=0 class=worm1></form>");
  106.  
  107. document.write("<br><font size=-1>Press any arrow key to start</font></td></tr></table>");
  108. var points = 0;
  109. var go = 1;
  110. var di = 0;
  111. var x = 0;
  112. var y = 0;
  113. var n = 0;
  114. document.images[1].src = "blank.gif"; 
  115. var blank = document.images[1].src;
  116. var hw = (height * width);
  117. var o = Math.floor(Math.random() * hw - 2);
  118. do {
  119. o = Math.floor(Math.random() * hw-2);
  120. } while(document.images[o].src != blank);
  121. var i = o;
  122. var food = 0;
  123. do {
  124. food = Math.floor(Math.random() * hw-2);
  125. } while (document.images[food].src != blank);
  126. document.images[i].src = "worm.gif";
  127. document.images[width-1].src="end.gif";
  128. var end = document.images[width-1].src;
  129. var file = document.images[i].src;
  130. var length = 1;
  131. var worm = new Array();
  132. var k = 0;
  133. var ie = document.all ? 1 : 0;
  134. var  enableScroll = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >3)) ;
  135. var height = document.images[0].height;
  136. var tScroll;
  137. var d = 0;
  138. function runTimer() {
  139. if (d != 0) { n++; }
  140. if (d == 1) { i--; }
  141. if (d == 2) { i++; }
  142. if (d == 3) { i += width; }
  143. if (d == 4) { i -= width; }
  144. if (document.images[i].src == end) {
  145. speed -= 400; i = worm[n-1]; di = 1; die();/*when the speed of the snake(worm)=400 which is stoping the worm will die*/
  146. }
  147. worm[n] = i;
  148. if(i == food) {
  149. length++; points += (10*length);/*when the snake(worm) eat food (i) you get points = to 10 times the length of the worm*/
  150. do {
  151. food = Math.floor(Math.random() * hw-2);/*we call Math.random method for the food to appear in random places every time you start the game*/
  152. } while (document.images[food].src != blank);
  153. if (di == 0) {
  154. document.info.elements[0].value = points;
  155.    }
  156. }
  157. if (n > length){
  158. o = worm[n-length];
  159. }
  160. if ((document.images[i].src == file) && (n > 1)) {
  161. speed -= 400; d = 0; di = 1; die();
  162. }
  163. if(di == 0) {
  164. document.images[o].src = "blank.gif";
  165. document.images[i].src = "worm.gif";
  166. document.images[food].src = "food.gif";
  167. tScroll = window.setTimeout("runTimer();", speed);/*used this method(setTimeout) to call tScroll function for a period of time*/
  168.    }
  169. }
  170. if (enableScroll){
  171. if (ie) window.onload = runTimer;/*when the function tScroll is called when the page loading the setTimeout method(runTimer) is enabled*/
  172. if (ie) window.onunload = new Function("clearTimeout(tScroll)");/*to stop the mwthod setTimeout by calling clearTimeout method*/
  173. }
  174. system = "";
  175. ver = navigator.appVersion;
  176. len = ver.length;
  177. for (iln = 0;iln < len; iln++) if (ver.charAt(iln) == "(") break;
  178. system = ver.charAt(iln+1).toUpperCase();
  179. document.onkeydown = keyDown;/*we called onKeydown function a keyDown*/
  180. if (system != "C") {
  181. document.captureEvents(Event.KEYDOWN);/*KEYDOWN method specifies the type of events that should be passed to the document rather than to the object for which they were intended */
  182. }
  183. function keyDown(DnEvents) {/*calling the event(keyDown)*/
  184. if (system != "C") {
  185. k = DnEvents.which;
  186. } else {
  187. k = window.event.keyCode;
  188. }
  189. if (k == 37) { d = 1; }
  190. if (k == 39) { d = 2; }
  191. if (k == 40) { d = 3; }
  192. if (k == 38) { d = 4; }
  193. }
  194. function die() {
  195. i = 0;
  196. o = 0;
  197. food = 0;
  198. document.info.elements[0].value = "Died with " + document.info.elements[0].value;
  199. var restart=confirm("Play Again?");
  200. if(restart){ location.reload(); }
  201. }
  202. function open_win()
  203. {
  204. window.open("snakegame.txt","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=500");
  205. }
  206. //]]>
  207. </script>
  208. </body>
  209.  
  210. </html>
Attached Images
File Type: jpg background.jpg (79.1 KB, 641 views)
File Type: gif worm.gif (1,010 Bytes, 187 views)
File Type: gif food.gif (933 Bytes, 187 views)
File Type: gif end.gif (49 Bytes, 190 views)
File Type: gif blank.gif (46 Bytes, 189 views)
Jan 4 '11 #1
1 2206
acoder
16,027 Expert Mod 8TB
First things first. Why test with IE first? Always test with another browser that is usually more standards-compliant.

What happens in the other browsers? What error message do you see? On which line?
Jan 11 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Steven Green | last post by:
I did a clean install of Windows XP on my computer and my javascript will not work. I've tried all types of browsers, I've download the Service pack 1 for XP and IE. I have download java from...
2
by: David | last post by:
On every web browser except Safari, this website works great. (Well, by "every" I mean Mozilla, Netscape, and Internet Explorer, for Mac and Windows). The site is: http://www.ruleofthirds.com ...
1
by: Mike | last post by:
Sorry if this isn't exactly the right NG for this, but I thought you guys would know better than anyone else. I have 2 computers networked. The internet connection is dial-up, and I'm using...
14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
1
by: Kuv patel | last post by:
Hi I am displaying a page inside an Iframe, and I would like to enlarge the iframe window to the size of the browser when the browser window loads up. In the onload event of the browser I am...
6
by: b. hotting | last post by:
Hi, I don't see why this won't work, it are 3 links, the last one (a get) does work, but the first 2 won't. i would like to use a post, through hidden input types any idea? thanks for your...
2
by: Sky Kast | last post by:
I'm running Firefox on a win 98 computer and javascript won't work And yes i have set everything javascript that should enable to true or check so it's not that
5
by: nickarnold | last post by:
Hi, this is my first time posting a question, so I apologize if I goof up or do not provide enough information. I am trying to implement a simple countdown script that I grabbed from Dynamic...
2
by: Matthew Wells | last post by:
Hello. I thought this was supposed to be simple. I'm trying to use a .js file for my javascript functions. I'm testing by only using one function The function works when it's at the top of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.