homepage Welcome to WebmasterWorld Guest from 54.166.108.167
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Problem with ' and "
bramley




msg:4475782
 4:24 am on Jul 15, 2012 (gmt 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 !

 

Fotiman




msg:4475829
 11:41 am on Jul 15, 2012 (gmt 0)

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 = '[...]';

daveVk




msg:4475830
 11:58 am on Jul 15, 2012 (gmt 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 ];

Fotiman




msg:4475831
 12:08 pm on Jul 15, 2012 (gmt 0)

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>"
]

bramley




msg:4475854
 2:37 pm on Jul 15, 2012 (gmt 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.

bramley




msg:4475867
 3:51 pm on Jul 15, 2012 (gmt 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

Fotiman




msg:4475868
 3:57 pm on Jul 15, 2012 (gmt 0)

Glad you got it working.

bramley




msg:4475915
 9:55 pm on Jul 15, 2012 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved