I am trying to get PHP to write some JS so I can manipulate an array client-side. Having worked though difficulties with escaping quotes and apostrophes, I seem to have hit a problem with 'http:// ’ because my JS is seeing the ‘//’ as a comment.
I tried escaping the ‘//’ as ‘\/\/’ but that gave me a link in the final HTML like 'http:\/\/example.com which tries to work as a relative not absolute URL. Same problem with the entity code ‘/’
That may be sufficient information for someone to tell me how to deal with the problem, but in case not, here’s a bit more detail:
The link starts life as part of a PHP variable:
Example WITH URL:
$phocr = "<a class='" . $phocrcolor . "' href='http://example.com/bird/' target='_blank'>© " . $year . " Nick Name</a>";
Example without URL (which works OK):
$phocr = '© ' . $year . ' Nick Name';
It’s then written (with other stuff) into a JS array using a PHP loop:
echo "img_array[" . $i . "] = '<img src=\\'pan/" . $panimgs[$keys[$i]] . ".jpg\\' alt=\\'\\' /><h1 class=\\'" . $hdgcolor . "\\'>Welcome to Mull & Iona</h1><h5 class=\\'" . $tagcolor . "\\'>" . $tagline . "</h5><p class=\\'phocr\\'>" . $phocr . "></p><h6 class=\\'imgcap\\'>" . $imgcaps[$keys[$i]] . "</h6>';\
";
I’ve left out a lot of detail here, suffice it to say there are other similar items, so I (should) end up with a JS array containing HTML statements. It works for those items that have the Copyright notice but don’t have the URL.
With the URL I get an error message in the Console: “Syntax Error: missing ; before statement”, which I’ve traced to the ‘//’ being taken as a comment.
When it works a typical JS array value looks like this:
img_array[2] = '<img src=\\'pan/marblequarry.jpg\\' alt=\\'\\' /><h1 class=\\'dark\\'>Welcome to Mull & Iona</h1><h5 class=\\'dark\\'>Mull - The Wildlife Capital of Scotland</h5><p class=\\'phocr\\'>© 2014 Tim Dawson></p><h6 class=\\'imgcap\\'>Old marble quarry, Iona</h6>';
The next job is to select which value to display according to a media query. I’ve yet to tackle that, but have a method in mind. I need to solve the problem above first.