• Resolved sjones6

    (@sjones6)


    When a caption includes an ‘, it breaks the lightbox because the inline JS string ends before it should since the apostrophe breaks the string. When attempting to evaluate the JS, the parser will throw an error since it can’t find the closing parenthesis. The following code fixed the issue for me. I add this on line 126 of maxgalleria-shortcode.php:

    return preg_replace('/'/', '\&#039', $output . $page_links);

    The HTML character ' evaluates to a single apostrophe, and all the RegEx is doing is escaping that so that when it’s output as a parameter in the JS function call, the string is not broken.

    https://wordpress.org/plugins/maxgalleria/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author AlanP57

    (@alanp57)

    Yes, using
    &#039
    in place of the apostrophe is the normal way to handle this issue.

    Thread Starter sjones6

    (@sjones6)

    Hi,

    Right. But perhaps I have no explained the issue clearly enough. The issue comes that &#039 is still rendered as a single quotation mark in the browser. Thus, the caption parameter in the markup is still interrupted by this ‘, which creates the JS error.

    The code above replaces the simple &#039 with \&#039, which is rendered with \’ so that the single quotation mark is escaped and the JS parameter is not broken.

    If I var_dump the string as it return from the shortcode, you can see it is this:

    <a onclick="javascript: mg_image_carousel_open_lightbox('PATH_TO_IMAGE', 'on', '307', 'NAME on know Princeton's campus in New Jersey.');

    That’s all well and good, but the browser interprets it as follows:

    <a onclick="javascript: mg_image_carousel_open_lightbox('PATH_TO_IMAGE', 'on', '307', 'NAME on Princeton's campus in New Jersey.'); return false;"><img src="PATH_TO_IMAGE" alt="" title=""></a>

    There’s the obvious problem that this string ‘NAME on Princeton’s campus in New Jersey.’ is broken by ‘ in Princeton’s. When clicking, this generates the following JS error since the string parameter is incorrect:

    Uncaught SyntaxError: missing ) after argument list

    since the string is now broken.

    It should be as follows:
    <a onclick="javascript: mg_image_carousel_open_lightbox('PATH_TO_IMAGE', 'on', '307', 'NAME on Princeton\'s campus in New Jersey.'); return false;"><img src="PATH_TO_IMAGE" alt="" title=""></a>.

    If you add a \ before ' within JS string parameters, it avoids this error. The code in my original post replaces &#039 with \ &#039 so that the single quotation mark is escaped in the browser.

    I’m re-opening this issue.

    Plugin Author AlanP57

    (@alanp57)

    I have fixed this issue in the image carousel plugin, version 1.1.7. In maxgalleria-image-carousel-template.php I have

    $caption = str_replace('\'', '\\\'', $attachment->post_excerpt);

    at line 174.

    Thread Starter sjones6

    (@sjones6)

    Much appreciated! I’ll update the plugin and ensure that this is fixed.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘JS Error with ' in Image Captions’ is closed to new replies.