Jump to content

Specifying The Values Of Key-value Pairs In An Object


iwato
 Share

Recommended Posts

QUESTION: Can you point me toward likely sources of error in the 1st subset of code?CODE DESCRIPTION: Please find below a single troublesome block of code and two subsets of code extracted from the original troublesome block. The first subset is identical with a portion of the code found in the original block and is unworkable. The second subset of code is nearly identical with a portion of the original code and works. These two subsets of the original code are provided to help you focus on the true source of my difficulty.The PROBLEM: The variable citation is a reference for an Object with one key-value pair. The value of this pair is suppose to be a string or HTML. In the first subset of code I have tried to combine the two. Unfortunately, it does not work. If I enter a simple string as indicated in the second subset of code, it works. What am I doing wrong?The TROUBLESOME BLOCK OF CODE

function citationPrompt() {	var pageTitle = window.opener.document.title;	var pageReference = window.opener.location.href;	var citationDate = $.toDate();	var citation = new Object();	citation = {pageCitation:		'<p>A proper way to cite a passage from this page:</p>		<p>Hashimori Iwato. 2000. <cite>Imagine:  Bridging Japan\'s Historical Gap</cite>.' + pageTitle + '. [online document] <' + pageReference + '> (' + citationDate + ').</p>' 	};	var temp = {		state0: {			html: citation.pageCitation,			buttons: {Continue: true},			focus: 1,			opacity:0.6,			submit:function(v,m,f){ 				if(v) {				return true;				}				return false; 			}		}	}				$.prompt(temp);}

The UNWORKABLE SUBSET

			var pageTitle = window.opener.document.title;	var pageReference = window.opener.location.href;	var citationDate = $.toDate();	var citation = new Object();	citation = {pageCitation:		"'<p>A proper way to cite a passage from this page:</p>		<p>Hashimori Iwato. 2000. <cite>Imagine:  Bridging Japan\'s Historical Gap</cite>.' + pageTitle + '. [online document] <' + pageReference + '> (' + citationDate + ').</p>'" 	};

The WORKABLE SUBSET

		var citation = new Object();	citation = {pageCitation:		"<p>A proper way to cite a passage from this page:</p>" 	};

Roddy

Link to comment
Share on other sites

The portion from the original and the first subset are NOT identical. The original has clearly delimited quoted literal concatenated with string variables. The subset has all that, but placed inside double quotations. That means that the single quotes and concatenation operators are treated as characters, not operators. Try getting rid of the double quotes. I think you'll be fine.FWIW: There is no need to initialize a variable as new Object() when the following statement uses {} syntax. They are equivalent. So the new Object() statement can safely be eliminated.

Link to comment
Share on other sites

The portion from the original and the first subset are NOT identical.
This is my posting error. Please accept my apology for the confusion.
The original has clearly delimited quoted literal concatenated with string variables. The subset has all that, but placed inside double quotations. That means that the single quotes and concatenation operators are treated as characters, not operators. Try getting rid of the double quotes. I think you'll be fine.
Unfortunately not. What follows is what I finally settled for. Please notice the complete absence of HTML code. Does the inability of the citation object to accept HTML code not probably lie in how the html key in the state0 object is defined?
function citationPrompt() {	var pageTitle = document.title;	var citationDate = new Date();	var pageReference = this.location.href;	var citation = new Object();	citation = {pageCitation: 'Hashimori Iwato. 2000. Imagine: Bridging Japan\'s Historical Gap. ' + pageTitle + '. <' + pageReference + '> (' + citationDate + ').'};	var temp = {		state0: {			html: citation.pageCitation,			buttons: {Continue: true},			focus: 1,			opacity:0.6,			submit:function(v,m,f){ 				if(v) {				return true;				}				return false; 			}		}	}				$.prompt(temp);}

FWIW: There is no need to initialize a variable as new Object() when the following statement uses {} syntax. They are equivalent. So the new Object() statement can safely be eliminated.
Although you are correct, I am still a beginner, and it helps me to understand my own humble attempts at coding. Surely, I must balance my fear of advancing too quickly with slower download times.As always, I thank you very much for your many helpful suggestions.Roddy
Link to comment
Share on other sites

More likely the problem is caused by the line break in the middle of your original string. When I eliminate that, I can alert temp.state0.html correctly.I must add that I have no idea how jQuery will respond to any of this.

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
 Share

×
×
  • Create New...