www.webdeveloper.com
Results 1 to 5 of 5

Thread: Problem with multiple quote levels (',") and dynamic variables

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Posts
    116

    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!

  2. #2
    Join Date
    Nov 2010
    Posts
    1,083
    can you just use single quotes (dunno why it works, so please don't ask... )

    Code:
    var videoString = 'Video 1 | 12345, Video \'Has quotation marks\' | 67890, Video 3 | 101112';

  3. #3
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    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.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by svidgen View Post

    ... Reason I ask, of course, is that it's much simpler to just use a more robust format. Something like JSON, for instance.
    Agree. Or XML.

  5. #5
    Join Date
    Feb 2006
    Posts
    116
    Quote Originally Posted by svidgen View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles