Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,149,869 members, 7,806,477 topics. Date: Tuesday, 23 April 2024 at 05:04 PM

Trouble With Javascript Code - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Trouble With Javascript Code (1728 Views)

Hlp With Javascript Snippet To Validate A User's Cell Phone Number Input / Javascript Code To Make Rolling Banners/images / How To Store Data With javascript / Javascript Cookie Workshop (2) (3) (4)

(1) (Reply) (Go Down)

Trouble With Javascript Code by IbroSaunks(m): 11:19am On Nov 07, 2011
Hi there!

So i have an issue with a javascript code. i kinda copied the code from a video tutorial im watching since im just learning javascript. the thing is, at first i typed the code because i find that can be useful sometimes, but after doing that, the code i typed didn't work. however, i did a copy and paste of the original code, and that worked!! strange! i debugged till 3:30am, after which i just moved on. So what could the problem be? here's the two of 'em, maybe im not seeing something:

window.onload = init;
var usedNums = new Array(76);

function init(){
if (document.getElementById){
document.getElementById("reload"wink.onclick = anotherCard;
newCard();
}
else{
alert("sorry, your browser does not support this script"wink;
}
}

function newCard(){
for(var i=0; i<24; i++){
setSquare(i);
}
}

function setSquare(thisSquare){
var currSquare = "square" + thisSquare;
var colSpace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);
var colBasis = colSpace[thisSquare] * 15;
var newNum;

do{
newNum = colBasis + getNewNum() + 1;

}
while(usedNums[newNum]);

usedNums[newNum] = true;
document.getElementById(currSquare).innerHTMl = newNum;
}
function getNewNum(){
return Math.floor ( Math.random() * 15);
}

function anotherCard(){
for(var i=1;i<usedNums.length;i++){
usedNums[i] = false;
}
newCard();
return false;
}
the faulty one is above


// JavaScript Document
window.onload = initAll;
var usedNums = new Array(76);

function initAll() {
if (document.getElementById) {
document.getElementById("reload"wink.onclick = anotherCard;
newCard();
}
else {
alert("Sorry, your browser doesn't support this script"wink;
}
}

function newCard() {
for (var i=0; i<24; i++) {
setSquare(i);
}
}

function setSquare(thisSquare) {
var currSquare = "square" + thisSquare;
var colPlace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);
var colBasis = colPlace[thisSquare] * 15;
var newNum;

do {
newNum = colBasis + getNewNum() + 1;
}
while (usedNums[newNum]);

usedNums[newNum] = true;
document.getElementById(currSquare).innerHTML = newNum;
}

function getNewNum() {
return Math.floor(Math.random() * 15);
}

function anotherCard() {
for (var i=1; i<usedNums.length; i++) {
usedNums[i] = false;
}

newCard();
return false;
}
and this is the ok one

also, it seems they are of different sizes. i really don't know what is going on. can anyone help
Re: Trouble With Javascript Code by Nobody: 11:28am On Nov 07, 2011
the browser doesn't support the init method only onload
Re: Trouble With Javascript Code by Nobody: 11:30am On Nov 07, 2011
unless u try
window['init']=function(){alert("Init Now Supported"wink}

window.init()

its obvious the browser must be an old browser to support init or the init is a function that was declared which u didn't see they cud be trying to explain Global Objects, because windows is a global object.
Re: Trouble With Javascript Code by Nobody: 11:33am On Nov 07, 2011
wait the code up is valid i didnt see the init function it works on my Console,what error do u get.
Re: Trouble With Javascript Code by yawatide(f): 12:29pm On Nov 07, 2011
1) The issue isn't the init fxn. The onload fxn is calling a user-defined init fxn. So again, this isn't the issue

2) First and foremost in times like this, you need to do what pc_guru has suggested - ask for what error OP is seeing. Anything otherwise is like asking a mechanic to determine what is wrong with a car without even knowing the symptoms. Sure the mechanic might get it right based on the sound of the car (assuming it even starts) but that is not scientific and in no way is a guarantee.

Happy coding!
Re: Trouble With Javascript Code by IbroSaunks(m): 12:46pm On Nov 07, 2011
i changed the function in the bugged on to init from initAll in a bid to get it working.

onload works for the other copied one, and other javascript code that i type. this is the first time its happening really.

theres no error, its just not working. the code is for some, "bingo" card thingi. let me paste the html code


<html>
<head>
<title>Make Your Own Bingo Card</title>
<link rel="stylesheet" rev="stylesheet" href="script.css" />
<script type="text/javascript" src="script_testt.js">
</script>
</head>
<body>
<h1>Create A Bingo Card</h1>
<table>
<tr>
<th width="20%">B</th>
<th width="20%">I</th>
<th width="20%">N</th>
<th width="20%">G</th>
<th width="20%">O</th>
</tr>
<tr>
<td id="square0">&nbsp;</td>
<td id="square1">&nbsp;</td>
<td id="square2">&nbsp;</td>
<td id="square3">&nbsp;</td>
<td id="square4">&nbsp;</td>
</tr>
<tr>
<td id="square5">&nbsp;</td>
<td id="square6">&nbsp;</td>
<td id="square7">&nbsp;</td>
<td id="square8">&nbsp;</td>
<td id="square9">&nbsp;</td>
</tr>
<tr>
<td id="square10">&nbsp;</td>
<td id="square11">&nbsp;</td>
<td id="free">Free</td>
<td id="square12">&nbsp;</td>
<td id="square13">&nbsp;</td>
</tr>
<tr>
<td id="square14">&nbsp;</td>
<td id="square15">&nbsp;</td>
<td id="square16">&nbsp;</td>
<td id="square17">&nbsp;</td>
<td id="square18">&nbsp;</td>
</tr>
<tr>
<td id="square19">&nbsp;</td>
<td id="square20">&nbsp;</td>
<td id="square21">&nbsp;</td>
<td id="square22">&nbsp;</td>
<td id="square23">&nbsp;</td>
</tr>
</table>
<p><a href="script.html" id="reload">Click here</a> to create a new card</p>
</body>
</html>
Re: Trouble With Javascript Code by Nobody: 1:51pm On Nov 07, 2011
The only way to go about this is to do this

1.on the HTML put '<body onload='init()>'' old school method

2.in the script aspect


//all browsers

var browserObject=(document.body.addEventListener /*Gecko Scripts*/|| document.body.attachEvent /*This is for IE*/);
browserObject('onload','function_to_run');/*They have the same param sig*/

Re: Trouble With Javascript Code by yawatide(f): 5:58pm On Nov 07, 2011
Also think about including alert statements everywhere, for the values you are expecting. If you hit an alert and it either doesn't show or it shows but not the result you are expecting, you know where the problem is coming from.

good luck!
Re: Trouble With Javascript Code by Nobody: 1:42am On Nov 08, 2011
I tweaked it slightly - http://.net/demos/bingo/index.html
You can download the source code from there.

It works well in all the browsers i tested it on - unless i dont know how it should run.
The script is quite straight forward - for anyone that is very vast in javascript.
Re: Trouble With Javascript Code by IbroSaunks(m): 7:12am On Nov 08, 2011
thank u much @yawa_ti_de and pc
Re: Trouble With Javascript Code by IbroSaunks(m): 7:15am On Nov 08, 2011
, guru
@dhtml, would download that once i get my modem up and running
the funny part is, the code worked in the other file perfectly. almost felt like some kinda virus was playing with me, thank u much though
Re: Trouble With Javascript Code by Nobody: 9:47pm On Nov 08, 2011
No problemo.
Re: Trouble With Javascript Code by Therages: 12:16am On Dec 04, 2011
I might be too late buh i'd like to add this extra spice @ Ibro Try using Array literals e.g
var arr = [1,2,2,3,5, ];
it will save u few extra bytes, and again You don't have to specify the Array length, its a sign of bad written code, because Arrays are always one higher dan the highest index so, new values can be added on the fly with mayb the Array push() method or arr[length] ="newVal"; I'm pretty sure its an old code buh take note Using the array constructor (new Array(2)) and asigning one (number)value to it javascript atomatically interpret it as the Array length.
I just hope i made sense.

(1) (Reply)

Get USA Phone Number, Address And Mail Forwarding / How To Win A Car In The New MTN App Developer's Challenge. / Codes & Tricks Using Wapka.mobi

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 35
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.