functions used before defined.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

functions used before defined.

Post by aravona »

I am a complete javascript beginner, but I code quite often in php, and in other software languages. I have a script which was a real mess. I ran it through an online validator and there was a lot wrong with it, no semi colons at all for one thing.

Code: Select all

// JavaScript Documentvar xmlHttp

function showSubjects(links,subjects){ 
	var url="show-subjects.php?sid=" + Math.random() + "&links=" + links + "&subjects=" + subjects;
	xmlHttp=GetXmlHttpObject(stateChanged);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);

} 

function stateChanged(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		document.getElementById("subjectdiv").value="";
		document.getElementById("subjectdiv").innerHTML=xmlHttp.responseText;

	} 
} 

function GetXmlHttpObject(handler){ 
	var objXmlHttp=null;

	if (navigator.userAgent.indexOf("Opera")>=0){
		alert("This example doesn't work in Opera");
		return;
	}
	if (navigator.userAgent.indexOf("MSIE")>=0){ 
	var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP";
		} 
	try	{ 
		objXmlHttp=new ActiveXObject(strName);
		objXmlHttp.onreadystatechange=handler ;
		return objXmlHttp;
		} 
	catch(e){ 
		alert("Error. Scripting for ActiveX might be disabled"); 
		return;
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0){
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler ;
		return objXmlHttp;
	}
}
The last thing the validator says is that ''stateChanged' was used before it was defined.' and ''GetXmlHttpObject' was used before it was defined.'

Now I know this means that the functions are being used before they are being defined, that much is completely obvious - but since this code was handed to me and not self written I'm not 100% sure how to tweak it. I know that its to work an optional drop down menu - but its not doing its job properly. Infact its altering a drop down menu that it shouldn't be changing.

Many Thanks,

Aravona
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: functions used before defined.

Post by JakeJ »

Try moving your ShowSubjects function down below the other two functions and try again. Then the other two functions will be defined before they are called by ShowSubjects.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: functions used before defined.

Post by aravona »

Ok that cleared up most of the errors but I'm still getting the following when I run the code through a validator:
Implied global: xmlHttp 3,5,42,43,44, document 4,5, navigator 13,17,19,32, alert 14,28, ActiveXObject 23, XMLHttpRequest 33
I assume this is why I'm still getting 'Done but with errors on the page'
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: functions used before defined.

Post by JakeJ »

Did you declare the proper document type for the html portion?
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: functions used before defined.

Post by aravona »

Its done on a php page which is called within the index.

The top of the index.php declaration is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo ADMINTITLE;?></title>
<link rel="stylesheet" media="screen" type="text/css" href="style.css" />
Looks like what dreamweaver churns out to me :) but I don't know what, if anything, would need changing for Javascript.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: functions used before defined.

Post by JakeJ »

I can't help you with the Javascript either. I suggest you repost the code in the javascript section or even a javascript forum, along with the header and the errors you're getting.

But first you might want to try looking up each of those errors. If you can hunt a couple of them down, you might learn some good stuff about javascript that will be useful in the future.

Good luck!
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: functions used before defined.

Post by aravona »

It IS in the javascript section.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: functions used before defined.

Post by JakeJ »

I was half asleep when i replied last night. HAHA I wasn't paying attention.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: functions used before defined.

Post by Weirdan »

I ran it through an online validator
Which one exactly? It seems it's not suitable for client-side javascript code running in a browser because those errors are not actually errors. For example, 'implied global' for document and navigator is nonsense - those variables are real globals, present in every major browser. 'Implied global' error for xmlHttp variable is a valid concern though - it's definition (var xmlHttp) is a part of comment for some reason.

'Function used before it is defined' is a total nonsense, because function declarations are parsed before execution starts (as per ECMA-262 3rd ed. Section 10.5).
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: functions used before defined.

Post by aravona »

I used JSlint.

The reasons I ran it through a validator in the first place are 1. I've only done a 2 week course in javascript so I'm no pro and 2. The website brings up 'done but with errors on page' whenever it runs the code and this is the only javascript called/used on the particular page. It also does not do what its supposed to do properly which is how I ended up at the validator in the first place. If that makes sense.
jerryvn01
Forum Newbie
Posts: 2
Joined: Thu Jul 08, 2010 9:26 am

Re: functions used before defined.

Post by jerryvn01 »

aravona wrote:I used JSlint.

The reasons I ran it through a validator in the first place are 1. I've only done a 2 week course in javascript so I'm no pro and 2. The website brings up 'done but with errors on page' whenever it runs the code and this is the only javascript called/used on the particular page. It also does not do what its supposed to do properly which is how I ended up at the validator in the first place. If that makes sense.
Hi,

I agreed with you. Any way, your ideal make me thinking about some thing for my project.

Apart from that, this link below may be useful: Web interview questions
Please try to keep posting. Tks and best regards
Last edited by jerryvn01 on Tue Aug 03, 2010 11:13 am, edited 1 time in total.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: functions used before defined.

Post by PHPHorizons »

Hello aravona,

I don't know if you're still looking for some answers on this, but I'll try and help you out a bit.

JSLint is meant to validate pure javascript code. The browser introduces things like document, navigator, and alert. In order to keep JSLint from complaining about these variables that you know are predefined, you can list them near the bottom of the JSLint page:

Predefined ( , separated) ________here________________

I'm not sure what the deal with the functions are, but there is a simple way to clear that up. Put a var statement at the top of your script that lists the variables.


var showSubjects, stateChanged, GetXmlHttpObject;
// then just put in those functions the same way you had them before, without worrying about the order, because as has been stated in this thread, javascript function declarations are hoisted to the top of their scope. (which simply means a function defined at the end of a script is essentially hoisted up to the top of the script before the rest of the script is parsed)

Cheers and I hope that helps.

PS I do highly recommend that you keep using JSLint. It is intended to be used with a browser executing client code, but it is also intended to be completely javascript'centric and thus does not assume window, document, etc.
Post Reply