Jump to content

A JS string writing problem


aleksanteri

Recommended Posts

I tried to do a thingy that creates XML tags, just for JS practise, but I got an error I can't get through. It says that I'm missing a semicolon...

<html><head><title>XML Builder</title><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" /><style type="text/css"><!--body {background-color : #eeeeee; color : #002f88; font-family : verdana, sans-serif;font-size : 12px; } a {background-color : #eeeeee; color : #0000ff; } a:visited {background-color : #eeeeee; color : #0000ff; } a:hover {color : #000000; background-color : #ddddff; } h1 {font-size : 30px; text-align : center; font-weight : 900; } h2 {font-size : 25px; text-align : center; font-weight : 700; } h3 {font-size : 22px; font-weight : 600; } h4 {font-size : 20px; font-weight : 300; } h5 {font-size : 17px; font-weight : 200; } h6 {font-size : 16px; font-weight : 150; } ul.nomarkers {list-style-type : none; }  img {border : 0; } th{background-color:#404040;color:#00FF00}--></style><script src="onerror.js" type="text/javascript"></script><script type="text/javascript"><!--function errorMessage(doesExist, caption){this.doesExist = doesExist;this.caption = caption;}function doXml(){	// get values	var element = document.XmlForm.element.value;	var attr1 = document.XmlForm.attr1.value;	var attr2 = document.XmlForm.attr2.value;	var attr3 = document.XmlForm.attr3.value;	var attr4 = document.XmlForm.attr4.value;	var attr5 = document.XmlForm.attr5.value;	var value1 = document.XmlForm.value1.value;	var value2 = document.XmlForm.value2.value;	var value3 = document.XmlForm.value3.value;	var value4 = document.XmlForm.value4.value;	var value5 = document.XmlForm.value5.value;	var inner = document.XmlForm.inner.value;		// validate element	if(element == "")	{  errorMessage.doesExist = 1  errorMessage.caption = "Your Xml element is not correct: \nYou have no element name!"	}	else if(element == "xml" || element == "XML" || element == "xMl" || element == "XMl" || element == "xmL" || element == "Xml" || element == "xMl" || element == "xML" || element == "XmL") // phew pretty long eh?	{  errorMessage.doesExist = 1  errorMessage.caption = "Your Xml element is not correct: \nYou shall not use \"" + element + "\" in your element name"	}		if(errorMessage.doesExist = 1)	{  alert(errorMessage.caption);	}		var attrnbr = new Array();	// let the comparation begin	if(attr1 != "")	{  attrnbr[1] = "true"	}	else	{  attrnbr[1] = "false"	}		if(attr2 != "")	{  attrnbr[2] = "true"	}	else	{  attrnbr[2] = "false"	}		if(attr3 != "")	{  attrnbr[3] = "true"	}	else	{  attrnbr[3] = "false"	}		if(attr4 != "")	{  attrnbr[4] = "true"	}	else	{  attrnbr[4] = "false"	}		if(attr5 != "")	{  attrnbr[5] = "true"	}	else	{  attrnbr[5] = "false"	}		var str;	str = "<" + element		if(attrnbr[1] = "true")	{  str = str + " "  str = str + attr1 '="' // ERROR LINE!!!  str = str + value1 + "\""	}		if(attrnbr[2] = "true")	{  str = str + " " + attr2 "=\"" + value2 + "\""	}		if(attrnbr[3] = "true")	{  str = str + " " + attr3 "=\"" + value3 + "\""	}		if(attrnbr[4] = "true")	{  str = str + " " + attr4 "=\"" + value4 + "\""	}		if(attrnbr[5] = "true")	{  str = str + " " + attr5 "=\"" + value5 + "\""	}		str = str + "></" + element + ">"}// --></script></head><body><h1>XML Builder</h1><form name="XmlForm"><table border="1" ><tr><th colspan="4"><p>Please fill in the form</p></th></tr><tr><td><input type="text" name="element" /></td><td colspan="3"><p>Element</p></td></tr><tr><td><input type="text" name="attr1" /></td><td><p>Attribute 1</p></td><td><input type="text" name="value1" /></td><td><p>Value 1</p></td></tr></tr><tr><td><input type="text" name="attr2" /></td><td><p>Attribute 2</p></td><td><input type="text" name="value2" /></td><td><p>Value 2</p></td></tr></tr><tr><td><input type="text" name="attr3" /></td><td><p>Attribute 3</p></td><td><input type="text" name="value3" /></td><td><p>Value 3</p></td></tr></tr><tr><td><input type="text" name="attr4" /></td><td><p>Attribute 4</p></td><td><input type="text" name="value4" /></td><td><p>Value 4</p></td></tr></tr><tr><td><input type="text" name="attr5" /></td><td><p>Attribute 5</p></td><td><input type="text" name="value5" /></td><td><p>Value 5</p></td></tr><tr><td><input type="text" name="inner" /></td><td colspan="2"><p>InnerHtml</p></td><td><input type="button" onclick="doXml()" value="Submit" /></td></tr><tr><td colspan="4"><input type="text" name="output" size="70" /></td></tr></table></form></body></html>

onerror.js

onerror=handleErrvar txt=""function handleErr(msg,url,l){txt="There was an error on this page.\n\n"txt+="Error: " + msg + "\n"txt+="URL: " + url + "\n"txt+="Line: " + l + "\n\n"txt+="Click OK to continue.\n\n"alert(txt)return true}

Can you fix the problem? I dunno how to do this...

Link to comment
Share on other sites

nice catch...i kept looking right at it and couldn't see it...i assume you noticed that the same error was repeated 4 more times.now i have a question...in the first line that generated the error, the quotes were handled correctly: '="'however, in the other lines, they appearned not to be: "=\"".yet, when i fixed the + omission errors, the code ran fine.how come? :)

Link to comment
Share on other sites

I had the problem when I put the "=\"" as the quote maker but when I got the error, I changed it to '="' but it didn't work. Now how I fix the problem? I have no idea... :)

Link to comment
Share on other sites

what browser are you trying it in? i fixed all of the quotes and have opened it in both firefox and ie with no problem.however, no matter what i enter, whenever i click the submit button, i always get a message box that says "undefined".any idea as to what's up?

Link to comment
Share on other sites

it's been a slow day so i've been fooling around with the code.fixed the various quote problems and == problems and added a line to write the results to the output box.see what you think.

<html><head><title>XML Builder</title><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" /><style type="text/css">body {background-color : #eeeeee;color : #002f88;font-family : verdana, sans-serif;font-size : 12px;}a {background-color : #eeeeee;color : #0000ff;}a:visited {background-color : #eeeeee;color : #0000ff;}a:hover {color : #000000;background-color : #ddddff;}h1 {font-size : 30px;text-align : center;font-weight : 900;}h2 {font-size : 25px;text-align : center;font-weight : 700;}h3 {font-size : 22px;font-weight : 600;}h4 {font-size : 20px;font-weight : 300;}h5 {font-size : 17px;font-weight : 200;}h6 {font-size : 16px;font-weight : 150;}ul.nomarkers {list-style-type : none;}  img {border : 0;}th{background-color:#404040;color:#00FF00}</style><script src="onerror.js" type="text/javascript"></script><script type="text/javascript"><!--function errorMessage(doesExist, caption){this.doesExist = doesExist;this.caption = caption;}function doXml(){// get valuesvar element = document.XmlForm.element.value;var attr1 = document.XmlForm.attr1.value;var attr2 = document.XmlForm.attr2.value;var attr3 = document.XmlForm.attr3.value;var attr4 = document.XmlForm.attr4.value;var attr5 = document.XmlForm.attr5.value;var value1 = document.XmlForm.value1.value;var value2 = document.XmlForm.value2.value;var value3 = document.XmlForm.value3.value;var value4 = document.XmlForm.value4.value;var value5 = document.XmlForm.value5.value;var inner = document.XmlForm.inner.value;// validate elementif(element == ""){ errorMessage.doesExist = 1 errorMessage.caption = "Your Xml element is not correct: \nYou have no element name!"}else if(element == "xml" || element == "XML" || element == "xMl" || element == "XMl" || element == "xmL" || element == "Xml" || element == "xMl" || element == "xML" || element == "XmL") // phew pretty long eh?{ errorMessage.doesExist = 1 errorMessage.caption = "Your Xml element is not correct: \nYou shall not use \"" + element + "\" in your element name"}if(errorMessage.doesExist == 1){ alert(errorMessage.caption);}var attrnbr = new Array();// let the comparation beginif(attr1 != ""){ attrnbr[1] = "true"}else{ attrnbr[1] = "false"}if(attr2 != ""){ attrnbr[2] = "true"}else{ attrnbr[2] = "false"}if(attr3 != ""){ attrnbr[3] = "true"}else{ attrnbr[3] = "false"}if(attr4 != ""){ attrnbr[4] = "true"}else{ attrnbr[4] = "false"}if(attr5 != ""){ attrnbr[5] = "true"}else{ attrnbr[5] = "false"}var str;str = "<" + elementif(attrnbr[1] == "true"){ str = str + " " str = str + attr1 + '="' str = str + value1 + '\"'}if(attrnbr[2] == "true"){ str = str + " " + attr2 + '=\"' + value2 + '\"'}if(attrnbr[3] == "true"){ str = str + " " + attr3 + '=\"' + value3 + '\"'}if(attrnbr[4] == "true"){ str = str + " " + attr4 + '=\"' + value4 + '\"'}if(attrnbr[5] == "true"){ str = str + " " + attr5 + '=\"' + value5 + '\"'}str = str + "></" + element + ">"document.XmlForm.output.value=str}// --></script></head><body><h1>XML Builder</h1><form name="XmlForm"><table border="1" ><tr><th colspan="4"><p>Please fill in the form</p></th></tr><tr><td><input type="text" name="element" /></td><td colspan="3"><p>Element</p></td></tr><tr><td><input type="text" name="attr1" /></td><td><p>Attribute 1</p></td><td><input type="text" name="value1" /></td><td><p>Value 1</p></td></tr></tr><tr><td><input type="text" name="attr2" /></td><td><p>Attribute 2</p></td><td><input type="text" name="value2" /></td><td><p>Value 2</p></td></tr></tr><tr><td><input type="text" name="attr3" /></td><td><p>Attribute 3</p></td><td><input type="text" name="value3" /></td><td><p>Value 3</p></td></tr></tr><tr><td><input type="text" name="attr4" /></td><td><p>Attribute 4</p></td><td><input type="text" name="value4" /></td><td><p>Value 4</p></td></tr></tr><tr><td><input type="text" name="attr5" /></td><td><p>Attribute 5</p></td><td><input type="text" name="value5" /></td><td><p>Value 5</p></td></tr><tr><td><input type="text" name="inner" /></td><td colspan="2"><p>InnerHtml</p></td><td><input type="button" onclick="doXml()" value="Submit" /></td></tr><tr><td colspan="4"><input type="text" name="output" size="70" /></td></tr></table></form></body></html>

:)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...