[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

How to debug js... find the bug.

I have two questions:

Q1: What is a proper way to debug js code? I have Fire Fox DOM inspector just in case.
Q2: Why alert("aaaa"); at line 46 is not firing?

Starting from the very top, I have moved alert("aaaa"); down to spot the portion of the code that is not working (does not fire alert("aaaa");).

Do you see what the problem is?

Thank you.
window.onload=initAll;
var xhr=false;
var xPos, yPos;

function initAll(){
	var allLinks = document.getElementsByTagName("a");
	
	for (var i=0; i<allLinks.length; i++){
		allLinks[i].onmouseover = showPreview;
	}
}

function showPreview(evt){
	if(evt) {
		var url=evt.target;
	}else{
		evt = window.event;
		var url=evt.srcElement;
	}
	xPos=evt.clientX;
	yPos=evt.clientY;
}

if (window.XMLHttpRequest) {
	xhr = new XMLHttpRequest();
}
else {
	if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) { }
	}
}

if (xhr) {
	xhr.onreadystatechange = showContents;
	xhr.open("GET", url, true);
	xhr.send(null);
}
else {
	alert("Sorry, but I couldn't create an XMLHttpRequest");
}

function showContents(){
alert("aaaa");
/*	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var outMsg = xhr.responseText;
		}
		else {
			var outMsg = "There was a problem with the request " + xhr.status;
		}

		// Do something here with outMsg
		var prevWin=document.getElementById("previewWin");
		prevWin.iinerHTML=outMsg;
		prevWin.stype.top=parseInt(yPos)+2 + "px";
		prevWin.stype.left=parseInt(xPos)+2 + "px";
		prevWin.stype.visibility="visible";
		
		prevWin.onmousout=function(){
			documnet.getElementByID("prevWin").style.visibility="hidden";
		}
	}
*/
}

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
1 Solution
 
Rainer JeschorCommented:
Hi,
I assume the closing "}" of line 22 should be in line 44:
window.onload=initAll;
var xhr=false;
var xPos, yPos;

function initAll(){
	var allLinks = document.getElementsByTagName("a");
	
	for (var i=0; i<allLinks.length; i++){
		allLinks[i].onmouseover = showPreview;
	}
}

function showPreview(evt){
	if(evt) {
		var url=evt.target;
	}else{
		evt = window.event;
		var url=evt.srcElement;
	}
	xPos=evt.clientX;
	yPos=evt.clientY;

if (window.XMLHttpRequest) {
	xhr = new XMLHttpRequest();
}
else {
	if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e) { }
	}
}

if (xhr) {
	xhr.onreadystatechange = showContents;
	xhr.open("GET", url, true);
	xhr.send(null);
}
else {
	alert("Sorry, but I couldn't create an XMLHttpRequest");
}
}
function showContents(){
alert("aaaa");
/*	if (xhr.readyState == 4) {
		if (xhr.status == 200) {
			var outMsg = xhr.responseText;
		}
		else {
			var outMsg = "There was a problem with the request " + xhr.status;
		}

		// Do something here with outMsg
		var prevWin=document.getElementById("previewWin");
		prevWin.iinerHTML=outMsg;
		prevWin.stype.top=parseInt(yPos)+2 + "px";
		prevWin.stype.left=parseInt(xPos)+2 + "px";
		prevWin.stype.visibility="visible";
		
		prevWin.onmousout=function(){
			documnet.getElementByID("prevWin").style.visibility="hidden";
		}
	}
*/
}

Open in new window

Otherwise the variable "url" is undefined.

HTH
Rainer
0
 
Rainer JeschorCommented:
And for debugging:
either FireBug, IEDev Tools or Iron (Chrome based browser).
Depending on the content/references sometimes the site jsfiddle.net
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
How do you use FireBug to check js?

It is not working yet. There is a new question at:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28137786.html

with the entire fiel uploaded to be tested.

Thanks,

Mike
0
 
Rainer JeschorCommented:
Hi Mike,
the console window in Firebug normally shows syntactical errors (missing ";",...).
For logical / workflow related "bugs" you normally set a breakpoint to the entry point (e.g. your initall function) and then step through the code, setting watches where necessary.

HTH
Rainer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now