View Full Version : Echoing JavaScript / Problems with Quotation Marks vs. Apostrophes

04-27-2012, 12:27 AM
Hello, everyone. I'm trying to move a lot of content that was embedded into a CMS into a functions file that ties into hooks. Everything's going pretty good except for my attempt at getting the below function to work.

Essentially, It's inserting an area with some social networking buttons. The Pinterest button uses JavaScript and my quotation marks are causing problems. I tried escaping, I tried switching to apostrophes, etc... and was able to get some things to work and others not.

Below is an echo with the raw HTML code without any of the things I've tried so you have a clear picture:

echo '<br /><div style="float:left;clear:left;height:35px;width:60px;"><a href="javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());"><img class="aligncenter" src="http://www.mysite.com/wp-content/uploads/2011/09/pinit.jpg" alt="Pin It" /></a></div><div style="float:left;clear:right;"><iframe src="//www.facebook.com/plugins/like.php?href='.the_permalink().'&amp;send=false&amp;layout=standard&amp;width=450&amp;show_faces=false&amp;action=like&amp; colorscheme=light&amp;font=arial&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:35px;" allowTransparency="true"></iframe></div><br /><br /></div>';

Just in case you're wondering, the error does not lie directly with the '.the_permalink.' part of the script as I did try removing that.

So can anyone tell me what to replace with apostrophes, what to escape with a backslash, etc...?

I really appreciate the help.

04-27-2012, 08:24 AM
IMHO one of the simplest solutions to the quotation mess (you have JS in HTML in PHP) would be separating languages. you can set the JS code in an external file, same goes for CSS, which leaves you with just HTML.

for a quick 'n' dirty fix you can use the HEREDOC syntax (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc).

04-27-2012, 04:17 PM
Thanks! Ultimately, I removed the inline CSS and added classes and managed the styles from the main stylesheet. I also removed the JavaScript and placed it in a separate function that loaded in the footer, and instead called the function from the HREF.

I appreciate the advice!