Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript function runs for 1 second then clears?
    primarykey
    data
    text
    <p>So I've written a web page with a few JavaScript functions.</p> <p>The page runs perfectly in Dreamweaver but when I tried it out in a browser (Google Chrome and Firefox) the JavaScript flashes for a split second then clears. I have no idea at all why this is. </p> <pre><code>&lt;body&gt; &lt;form name="myForm"&gt; &lt;ol&gt; &lt;li&gt; &lt;h1&gt; TILE CALCULATOR&lt;/h1&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="wall height"&gt;Wall height (cm)&lt;/label&gt; &lt;input type="text" id="wall_height" /&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="wall width"&gt;Wall Width (cm)&lt;/label&gt; &lt;input type="text" id="wall_width" /&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="tile height"&gt;Tile Height (cm)&lt;/label&gt; &lt;input type="text" id="tile_height" /&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="tile width"&gt;Tile Width (cm)&lt;/label&gt; &lt;input type="text" id="tile_width" /&gt; &lt;/li&gt; &lt;button onclick="javascript:validate();"&gt; Calculate &lt;/button&gt; &lt;/ol&gt; &lt;/form&gt; &lt;br /&gt; &lt;p id="result"&gt;&lt;/p&gt; &lt;br /&gt; &lt;canvas id="myCanvas"&gt; Your browser does not support this feature&lt;/canvas&gt; &lt;br /&gt; &lt;br /&gt; &lt;script language="javascript" type="text/javascript"&gt; //functoin to validate the inputs by the user //user can only enter a number and all fields must be filled function validate() { //first make sure canvas is clear clearCanvas() //take the inputs as variables var x = document.getElementById("tile_width").value; var y = document.getElementById("tile_height").value; var z = document.getElementById("wall_width").value; var i = document.getElementById("wall_height").value; //check if the user has entered nothing and alert if they have if (x==null || x=="" || y==null || y=="" || z==null || z=="" || i==null || i=="") { alert("All the fields have to be filled out!"); clearResult(); } // check if the user has entered invalid values, only numbers can be entered if (isNaN(x) == true || isNaN(y) == true || isNaN(z) == true || isNaN(i) == true) { alert("Dimensions can only be numbers!"); clearResult(); } //check for negatives if (x &lt;= 0 || y &lt;= 0 || z &lt;= 0 || i &lt;= 0) { alert("invalid dimension input, positive non-zero values only"); clearResult(); } //if valid calculate tiles and print else tileCalculator(); } function tileCalculator() { //take the input as variables var tileWidth = document.getElementById("tile_width").value; var tileHeight = document.getElementById("tile_height").value; var wallWidth = document.getElementById("wall_width").value; var wallHeight = document.getElementById("wall_height").value; //find the areas of the tile and the wall var tileArea = tileWidth * tileHeight; var wallArea = wallWidth * wallHeight; //divide these to find the number of tiles needed var noOfTiles = (wallArea/tileArea); //prints the result of noOfTiles document.getElementById("result").innerHTML=" The number of Tiles you will need are : " + noOfTiles; //scalled tiles to the canvas width of your choice var scalledWidth = 500; var ratioHW = wallHeight/wallWidth; var scalledHeight = ratioHW*scalledWidth; //scaled tile sizes //scale the tiles to correct pixels var scalledTileWidth = (tileWidth/wallWidth)*scalledWidth; var scalledTileHeight = (tileHeight/wallHeight)*300; //finds the number of tiles needs in a row var noWidth = wallWidth/tileWidth; //number of tiles in a column var noHeight = wallHeight/tileHeight; var canvas = document.getElementById("myCanvas"); canvas.style.width=scalledWidth + "px"; canvas.style.height=scalledHeight + "px"; printWall(0,0,noWidth,scalledTileWidth,(scalledTileHeight/2),noHeight); } //print a tile given the position and dimensions function printTile(x,y,tileWidth,tileHeight) { var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.fillRect(x,y,tileWidth,tileHeight); } //prints one row of tiles given the starting position and number to be printed function printTileRow(x,y,numberOfTiles,tileWidth,tileHeight) { var start = 0; //loops upto number of tiles in a row while (start &lt; numberOfTiles) { //prints a tile each time printTile(x,y,tileWidth,tileHeight); //next brick position x = x + (tileWidth + 1); // add a space between tiles here. start++; } } //prints the wall function printWall(x,y,numberOfTiles,tileWidth,tileHeight,numberOfRows) { //holds whether last row was shifted var shiftCount = 0; //starting index var start = 0; //loop up adding a row until required number of rows while (start &lt; numberOfRows) { //prints half a tile at the start of each row printTile(0,y,(0.5 * tileWidth - 1),(tileHeight)); //prints the row printTileRow((x+shiftCount),y,numberOfTiles,tileWidth,tileHeight); //if shifted if (shiftCount &gt; 0) { //was shifted last row shiftCount = 0; } else { //was not shifted last row shiftCount = shiftCount + (0.5*tileWidth); } start++; //start next row y = y + (tileHeight + 1); } } //clears the canvus each time the button is pressed function clearCanvas() { //reset canvus to 300 by 300 and clear var canvas = document.getElementById("myCanvas"); var context = canvas.getContext('2d'); canvas.style.height="300px"; canvas.style.width="300px"; context.clearRect(0,0,300,300); } function clearResult() { document.getElementById("result").innerHTML=""; } &lt;/script&gt; &lt;/body&gt; </code></pre> <p>I would really appreciate it if someone could have a quick look for me! Thanks, </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload