1

I'm trying to embed an instance of jwplayer in an entry. The embed code for that looks something like this:

<div id='my-video'></div>
<script type='text/javascript'>
    jwplayer('my-video').setup({
        file: 'url_to_file',
        image: 'url_to_image',
        width: '320',
        height: '240'
    });
</script>

I was just wondering if there is anyway to keep the formatting set to XHTML and still have it not convert the straight quotes between the initial set of parenthesis into entities. Here is a snippet of the problem area:

<div id='my-video'></div>
<script type='text/javascript'>jwplayer(&#8216;my-video&#8217;).setup({…

Further Information: I'd really like to get this worked out so let me clarify a few points here to try to make everything as clear as possible.

  • Only the first set of quotes inside jwplayer('my-video') are getting converted, all the rest of the quotes are left alone. Nothing I've tried in changing the formatting works. In auto br mode, taking out the whitespace causes it to stop being mangled.
  • I need to be able to add the embed code in the entry itself, not in the template code.
  • I would prefer not to install an add-on to do it, but having already installed stash and allow-ee-code in the process of trying to get it worked out demonstrates that I'm willing if it is necessary.
1

There is a config variable called "protect_javascript" which needs to be set to "no".

$config['protect_javascript'] = 'n';

EDIT: You can also use Stash to solve this issue too.

{exp:stash:set name="jwplayer"}
    jwplayer({my-video}).setup({...});
{/exp:stash:set}

Then you would use the get tag and output this parsed data within a script tag.

<script>
    {exp:stash:get name="jwplayer"}
</script>
| improve this answer | |
  • Justin, I was thinking that maybe you were going to be my savior on this, because I had gone straight to this config option, but had been trying setting it to 'y'. Unfortunately, I just set it to 'n' and seem to get exactly the same output. – UltraBob Jul 25 '13 at 6:45
  • Hm... I am not sure why it's not working for you, but I can tell you for 100% certainty this is the correct option. I use this same method within my Google Maps for EE add-on which does the exact same thing (essentially). I have worked around your issues with Stash before too, I will edit my answer to provide that approach. – Justin Kimbrell Jul 25 '13 at 14:16
  • Thanks Justin, I'd love it if you wouldn't mind sticking that embed code into a text area field set to xhtml formatting, with protect javascript set to off and do a view source on the resulting page to see how it comes out for you. For me, all of the straight quotes are ok, except for the first two in the parenthesis. I will look into the stash approach too. – UltraBob Jul 26 '13 at 2:05
  • Being inexperienced with stash it took me a little time to look at it in enough depth, but I don't think it works for me. If at all possible I need to be able to define the embed code in the entry. I want as far as enabling allow_ee_code on the entry so that I could define the stash:set bit in the entry, but then I end up with stash outputting text munged exactly as it was in the original post. It would be really helpful if someone could verify that they see the same behavior with protect_javascript set to no. – UltraBob Jul 29 '13 at 8:45
0

The quotes in the jwplayer call are not the quotes you need (8220 and 8221 are not the same characters you used in the rest of the code). Simply replacing these again with straight quotes and updating your entry should work.

| improve this answer | |
  • The mismatched quote type and entity were just a bad paste as a result of experimentation to try to eliminate the issue (I switched to double quotes instead of single quotes to see if that would clear up the issue.) Thanks for pointing it out, I'll update the original post to eliminate that source of confusion. – UltraBob Jul 29 '13 at 7:19
0

After a lot of experimentation I determined that everything inside the script tags inside a set of curly brackets was safe from being converted to entities. (This seems to be true with or without protect_javascript turned on)

Given that information, here was my solution:

<div id='my-video'></div>

<script type='text/javascript'>
function placeVideo() 
{
jwplayer('my-video').setup({
        file: 'url_to_file',
        image: 'url_to_image',
        width: '480',
        height: '360'
    });

}
placeVideo();
</script>

I had to wrap the call to the jwplayer function inside another function so that there would be curly brackets around the quotes.

| improve this answer | |
  • 1
    you might want to consider if placing javascript code in a html-textarea is a good way to add video to entries. Just storing the parameters in some grid like fieldtype is also a way to separate concerns. – GDmac Aug 2 '14 at 2:03
  • In this case it was kind of needed, to be able to place the video anywhere in the other text that was desirable on a case-by-case basis. – UltraBob Sep 13 '14 at 13:00
  • 1
    If you ever want to replace jwplayer with something else, you're in for a jolly afternoon plucking thru entries. Separation of concerns pattern alleviates this. Maybe worth to look at 'content elements' or 'entry widgets'. – GDmac Sep 13 '14 at 13:46
0

I have found that formatting the javascript as strictly-compliant XHTML will prevent the Auto XHTML parser from converting the JS quotes to html entities and breaking the scripts.

Here are my tests and the results for each.

1 -- Nope

<script>load_slides_from("Some Text. ‘smart’ “quotes” are OK.");</script>

2 -- Nope

<script>
load_slides_from("Some Text. ‘smart’ “quotes” are OK.");
</script>

3 -- Nope

<script type="text/javascript">
  load_slides_from("Some Text. ‘smart’ “quotes” are OK.");
</script>

4 -- This works

<script type="text/javascript">
 //<![CDATA[
  load_slides_from("Some Text. ‘smart’ “quotes” are OK.");
 //]]>
</script>

5 -- This works

<script>
 //<![CDATA[
  load_slides_from("Some Text. ‘smart’ “quotes” are OK.");
 //]]>
</script>

6 -- This works

<script>//<![CDATA[
  load_slides_from("Some Text. ‘smart’ “quotes” are OK.");
//]]></script>

7 -- This works

<script>//<![CDATA[
  load_slides_from("Some Text. ‘smart’ “quotes” are OK.");//]]></script>

Since it is often the case that you will want to use Javascript that does NOT include curly brackets on their own line I think this is a more generally useful solution.

| improve this answer | |
  • Thanks for this, it has been a while, but I don't think the curly brackets had to be on their own line, the point was to have then encountered by the parser before it reached the javascript it would mangle. I put them on a separate line to make it easier to understand for the reader. It looks like using //<![CDATA[ works in much the same way. – UltraBob Sep 13 '14 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.