Hey,
I have started learning Javascript programming, I wrote these two codes and it thy are not working, it shows a blank page when i load those files. Please help.

<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
function card(name, address, work, homep)
{
	this.name=name;
	this.address=address;
	this.workphone=work;
	this.homephone=homep;
	this.PrintCard=PrintCard;
}
function PrintCard()
{
	line1="Name : "+this.name+"<br />";
	line2="Address : "+this.address+"<br />";
	line3="Work : "+this.workphone+"<br />";
	line4="Home : "+this.homephone+"<br />";
	document.write(line1, line2, line3, line4);
}
tom=new Card("Tom Jones","123 Elm Street","9810669558","27355558");
tom.PrintCard();
</script>
</body>
</html>
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
function addHead(level){
	html='H'+level;
	text=this.toString();
	start="<"+html+">";
	stop="</"+html+">";
	return start+text+stop;
}
String.prototype.heading=addhead;
document.write("This is a headding 1".heading(1));
document.write("This is a heading 2".heading(2));
document.write("This is a heading 3".heading(3));
</script>
</body>
</html>

Depending on what browser you are using, I'd recommend installing a browser extension so that you can get some sort of log message to show what part of your js code is failing.

I use the developer tools compatible with google chrome. Firebug is another good alternative.

In addition, you can use this great code quality tool called JSLint which will provide you with excellent code analysis and prevent you from falling into bad practices when coding your javascript.

Member Avatar for stbuchok

Two words, "Case Sensitive".

addHead != addhead
Card != card

Remember to declare local variables (inside functions), otherwise you will create globals:

function addHead(level){
	var html='H'+level;
	var text=this.toString();
	var start="<"+html+">";
	var stop="</"+html+">";
	return start+text+stop;
}

Even better, do it in one line; local variables not required:

function addHead(level){
	return "<h%s1>%s2</h%s1>".replace(/%s1/g,level).replace("%s2",this.toString());
}

Airshow

Two words, "Case Sensitive".

addHead != addhead
Card != card

Thanks. It was the case sensitivity that did not execute the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.