Welcome to WebmasterWorld Guest from 54.159.19.75

Forum Moderators: open

Message Too Old, No Replies

Problem with ' and "

     
4:24 am on Jul 15, 2012 (gmt 0)

Junior Member

joined:Mar 30, 2011
posts: 153
votes: 0


I am converting a script from PHP to javascript and am completely stuck on how to write this line correctly:

si='["http://img.youtube.com/vi/'+SSv[i]+'/0.jpg\' height=\''+hhh+'\' width=\''+www+'\' class=\'preview\' title=\''+SSt[i]+'\' onclick=\'vidtxt=\"'+SSd[i]+'\";openVideo(\"'+SSv[i]+'\",\"'+SSt[i]+'\");\' style=\'cursor:pointer;\'", "", "", "<span style=\'color:#fff733;cursor:pointer;\' onclick=\'openVideo(\"'+SSv[i]+'\",\"'+SSt[i]+'\");\'><span style=\'font-weight:bold;font-size:1.1em;color:#fff;\'>'+SSt[i]+'</span></span>"]';


I worked on the completed string to get it to work and it is as follows:

['http://img.youtube.com/vi/3tqS8eGWwdg/0.jpg" height="365" width="480" class="preview" title="title 1" onclick="vidtxt=\'desc 1\';openVideo(\'3tqS8eGWwdg\',\'title 1\');" style="cursor:pointer;"', "", "", "<span style='color:#fff733;cursor:pointer;' onclick='openVideo(\"3tqS8eGWwdg\",\"title 1\");'><span style='font-weight:bold;font-size:1.1em;color:#fff;'>title 1</span></span>"]


But when I try to get the string as a variable, with live parameters added in, I just can't get it right. After so many hours, it's driving me insane, and I need help.

It is basically an array of four strings (the middle 2 are empty): ["","","",""]

One caveat:

If one looks at the good code (2nd one) it may look odd but the first (of 4 strings) in the array is later set as the src for an image and I am adding in height etc. hence when src='a' is carried out it will actually be like src="'a' height='h'"

The PHP version which worked :

$si= '["http://img.youtube.com/vi/'.$va[$i].'/0.jpg\" height=\"'.$hhh.'\" width=\"'.$www.'\" title=\"'.$tx[$i].'\" onclick=\'vidtxt=\"'.$tx[$i].'\";openVideo(\"'.$va[$i].'\",\"'.$ta[$i].'\");\' style=\'cursor:pointer;\' ", "", "", "<span style=\'color:#fff733;cursor:pointer;\' onclick=\'openVideo(\"'.$va[$i].'\",\"'.$ta[$i].'\");\'><span style=\'font-weight:bold;font-size:1.1em;color:#fff;\'>'.$ss[$i].'</span></span>"]';


Yet the exact equivalent in javascript does not work :

si='["http://img.youtube.com/vi/'+SSv[i]+'/0.jpg\" height=\"'+hhh+'\" width=\"'+www+'\" class=\"preview\" title=\"'+SSt[i]+'\" onclick=\'vidtxt=\"'+SSd[i]+'\";openVideo(\"'+SSv[i]+'\",\"'+SSt[i]+'\");\' style=\'cursor:pointer;\'", "", "", "<span style=\'color:#fff733;cursor:pointer;\' onclick=\'openVideo(\"'+SSv[i]+'\",\"'+SSt[i]+'\");\'><span style=\'font-weight:bold;font-size:1.1em;color:#fff;\'>'+SSt[i]+'</span></span>"]';


tia !
11:41 am on July 15, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


It is basically an array of four strings

Is it an array you want, or a string that would eval to an array? I ask because your JavaScript example looks more like that latter:

si = '[...]';
11:58 am on July 15, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 0


if si is an array rather than a string
si = '[ ... ]';
should be
si = [ ... ];

Consider breaking it up a bit say

var a='http://img.youtube.com/vi/' + SSv[ i ] + '/0.jpg"'
+ ' height="' + hhh + '"'
+ ' width="' + www + '"'
... etc
var b= ...
si = [ a,'','',b ];
12:08 pm on July 15, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


I'd just start breaking it down into the smaller pieces. If you're having a hard time, you might put each string into its own variable until you get it working, and then combine them all once you've got it figured out (reduces the amount of stuff you need to look at). Or start with the completed string and then work backwards, putting each concatenation on it's own line. For example, here's what you said was the desired completed string (note, I'm assuming that you're actually looking for an array here and not a string containing an array):


si = [
'http://img.youtube.com/vi/3tqS8eGWwdg/0.jpg" height="365" width="480" class="preview" title="title 1" onclick="vidtxt=\'desc 1\';openVideo(\'3tqS8eGWwdg\',\'title 1\');" style="cursor:pointer;"',
"",
"",
"<span style='color:#fff733;cursor:pointer;' onclick='openVideo(\"3tqS8eGWwdg\",\"title 1\");'><span style='font-weight:bold;font-size:1.1em;color:#fff;'>title 1</span></span>"
]


So start substituting:

si = [
'http://img.youtube.com/vi/' +
SSv[i] +
'/0.jpg" height="' +
hhh +
'" width="' +
www + '" class="preview" title="' +
SSt[i] +
'" onclick="vidtxt=\'' +
SSd[i] +
'\';openVideo(\'' +
SSv[i] +
'\',\'' +
SSt[i] +
'\');" style="cursor:pointer;"',
"",
"",
"<span style='color:#fff733;cursor:pointer;' onclick='openVideo(\"" +
SSv[i] +
"\",\"" +
SSt[i] +
"\");'><span style='font-weight:bold;font-size:1.1em;color:#fff;'>" +
SSt[i] +
"</span></span>"
]
2:37 pm on July 15, 2012 (gmt 0)

Junior Member

joined:Mar 30, 2011
posts: 153
votes: 0


Hi DaveVk and Fotiman,

Just woke up and about to start on it again. I'll look at your replies again after a coffee. I was partially wondering if string concatenation and ' / " ordering worked differently somehow in javascript.

Maybe I should add that this array of strings is part of a script that is appended to the document body.
3:51 pm on July 15, 2012 (gmt 0)

Junior Member

joined:Mar 30, 2011
posts: 153
votes: 0


I've managed to get it going.

I needed to double escape at the second level as single escaping only was lost in the appending.

So rather than ' " \', ' " \\'

Now the easy bit is done, on with the JSON part ;)

Thanks again
3:57 pm on July 15, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


Glad you got it working.
9:55 pm on July 15, 2012 (gmt 0)

Junior Member

joined:Mar 30, 2011
posts: 153
votes: 0


Thanks FoTiMan ! Took all day, but just now finished the json bits as well :)