ggwicz
ggwicz

Reputation: 568

Adding the almighty CDATA tag to some PHP-echoed jQuery

attached at the bottom of this question is some code that's being echoed out in a PHP-based Wordpress plugin. I need to figure out a way to make sure this javascript is wrapped in CDATA, becuase it's interfering with the RSS feed generator. Any help would be appreciated, I've tried several ways and it's not really working correctly. Everything works great with the plugin except for the RSS feed problem :/ Thanks!!

        echo '<div id="flickr-images">';    

                 echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>';
                 echo '<script type="text/javascript">';

                 echo   ' $(document).ready(function() {'; 
                 echo '$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=' .$userid. '&format=json&jsoncallback=?", function(data) {';
                 echo '    var target = "#flickr-images";'; 
                 echo '      for (i = 1 ; i <=' .$numpics .'; i = i + 1) {';  
                 echo '                 var pic = data.items[i];';
                 echo '                 var liNumber = i + 1;'; ?> 
                                $(target).append("<li class='hi-flickr-image hif-no-" + liNumber + "'><a title='" + pic.title + "' href='" + pic.link + "'><img src='" + pic.media.m + "' /></a></li>");
                            }
                           });
                        });

                 </script>


         <?php  echo '</div>';

Upvotes: 1

Views: 2065

Answers (1)

ElonU Webdev
ElonU Webdev

Reputation: 2459

Either I don't understand the question, or this is what you're trying to do:

echo '<![CDATA[';

// all the javascript echoed here

echo ']]>';

EDIT

I assume you're aware the code above is missing some echo statements? Some single quotes in the jquery parts also looked like it was throwing it off. This php didn't execute, did it? Anyways, see if this works, but I may have missed a few quotes somewhere...

echo '<div id="flickr-images">';    

echo '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>';
echo '<script type="text/javascript">';

echo '<![CDATA[';

echo ' $(document).ready(function() {'; 
echo '$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=' .$userid. '&format=json&jsoncallback=?", function(data) {';
echo '    var target = "#flickr-images";'; 
echo '      for (i = 1 ; i <=' .$numpics .'; i = i + 1) {';  
echo '                 var pic = data.items[i];';
echo '                 var liNumber = i + 1;'; ?> 
echo '                 $(target).append("<li class=\'hi-flickr-image hif-no-" + liNumber + "\'><a title=\'" + pic.title + "\' href=\'" + pic.link + "\'><img src=\'" + pic.media.m + "\' /></a></li>")';
echo '       }';
echo '       });';
echo '    });';

echo ']]>';

echo '</script>';


echo '</div>';

Upvotes: 1

Related Questions