Problem with multiple quote levels (',") and dynamic variables
I have a feeling this is way more simple than I'm making it, but I've spent the last several hours trying to figure it out to no avail, so here we are...
I've got a page that establishes list of variables as a string of comma separated values. I then take that list, parse it into an array using .split(',") and work with the respective values that way. Works great....
However, I find that if one of the values assembled into that string contains quotes, it breaks. Luckily, I can control the values that get put into that comma separated string and I tried adding a leading slash (\) before the quotes so they get ignored, but some how they still manage to get picked up when I make them variables and insert them later and pass them to a tooltip function
Here's my code:
Code:
//my starting string
var videoString = 'Video 1 | 12345, Video "Has quotation mark\'s" | 67890, Video 3 | 101112';
//split string into an array
var productVideos = new Array();
var productVideos = videoString.split(",");
for (video in productVideos) {
//isolate video ID and title, which are used separately
var node = String(productVideos[video]);
var titleEnd = node.indexOf("|") - 1;
var tempTitle = node.substr(0 ,titleEnd);
var idStart = titleEnd+3;
var tempID = node.substr(idStart);
//assemble link to Vimeo
var vidLink="http://vimeo.com/" + tempID;
//when tempTitle contains quotes, this string throws errors. title and alt tags on the image also fail
videoLinks += '<a href="' + vidLink + '" id="thumb' +tempID + '" title="' + tempTitle + '" onmouseover="tooltip.show(\'' + tempTitle + '\');" onmouseout="tooltip.hide();"><img id="img' +tempID + '" title="' + tempTitle + '" alt="' + tempTitle + '" src="images/video-thumbs/video.jpg"></a>';
}
//assemble links into preestablished container
container.innerHTML = videoLinks;
So, how can I allow videoString to contain values with quotations in them?
I checked out this link here, but it only seems to only be concerned with values that have commas in them, which mine are unlikely to have.
Any suggestions would much much appreciated. Thanks!
Where's the string coming from? Are you generating it server-side and stuffing it into the code? Returning it from a web service of some sort? Etc. ... Or is this something you're completely not in control of?
... Reason I ask, of course, is that it's much simpler to just use a more robust format. Something like JSON, for instance.
Where's the string coming from? Are you generating it server-side and stuffing it into the code? Returning it from a web service of some sort? Etc. ... Or is this something you're completely not in control of?
Well, long story short, it's being generated by my content management system (NetSuite). I really couldn't tell you what format it's in, but I do have the ability to edit what the content of each title is. So, bascially, if I have a video with a title that has quotes, I can enter it just about any way I want to:
This is my string "that contains quotes." It's pretty awesome.
This is my string 'that contains quotes.' It's pretty awesome.
"This is my string 'that contains quotes.' It's pretty awesome."
Full disclosure: I haven't had time to test out the first suggestion in this thread, but it did give me the idea of using single quotes all around, which could certainly simplify things.
Bookmarks