Special Characters Within Javascript
Posted: Fri Jan 27, 2012 11:41 pm
Hello,
I'm having an issue printing a string held in a PHP variable, within JavaScript. I am dynamically generating a keyword value for the Amazon MP3 widget, but for some reason apostrophes in the string will not escape within JavaScript. Here is the code:
$dynamicallygeneratedvariable is a string that contains quotes. In the working example below, the actual string is "Beethoven's Trinklied No. 282".
See the problem in action:
http://www.drinking-songs.com/beethoven ... ied-no-282
Notice how the widget title on top of the right sidebar does not contain the apostrophe? Instead it is being replaced by the ASCII code. I have tried all the decode functions for PHP with no success.
Results outside of JavaScript:
"Beethoven's Trinklied No. 282
Results within JavaScript:
Beethoven’s Trinklied No. 282
Here is the JavaScript code that produces the widget:
<script type='text/javascript'>
var amzn_wdgt={widget:'MP3Clips'};
amzn_wdgt.tag='hotogestjo-20';
amzn_wdgt.widgetType='SearchAndAdd';
amzn_wdgt.ASIN='B0011Z0YR2,B00137W4P8,B0013G0PG4,B001AU8ZLK,B001AUCJZ8,B001AUEMDK,B001AU8YB6,B001AU8YBQ,B001AU8YCK,B001AUCK2U,B001AUEMFS,B001AUCK52,B001AU6XE6,B001AUEMH6';
amzn_wdgt.keywords='<?php echo $keyword; ?>';
amzn_wdgt.title='<?php echo $keyword; ?>';
amzn_wdgt.width='250';
amzn_wdgt.height='250';
amzn_wdgt.shuffleTracks='True';
amzn_wdgt.marketPlace='US';
</script>
<script type='text/javascript' src='http://wms.assoc-amazon.com/20070822/US ... ect_1_5.js'>
</script>
Any help is appreciated. Thanks!
I'm having an issue printing a string held in a PHP variable, within JavaScript. I am dynamically generating a keyword value for the Amazon MP3 widget, but for some reason apostrophes in the string will not escape within JavaScript. Here is the code:
Code: Select all
<?php
$keyword = $dynamicallygeneratedvariable;
?>
See the problem in action:
http://www.drinking-songs.com/beethoven ... ied-no-282
Notice how the widget title on top of the right sidebar does not contain the apostrophe? Instead it is being replaced by the ASCII code. I have tried all the decode functions for PHP with no success.
Results outside of JavaScript:
"Beethoven's Trinklied No. 282
Results within JavaScript:
Beethoven’s Trinklied No. 282
Here is the JavaScript code that produces the widget:
<script type='text/javascript'>
var amzn_wdgt={widget:'MP3Clips'};
amzn_wdgt.tag='hotogestjo-20';
amzn_wdgt.widgetType='SearchAndAdd';
amzn_wdgt.ASIN='B0011Z0YR2,B00137W4P8,B0013G0PG4,B001AU8ZLK,B001AUCJZ8,B001AUEMDK,B001AU8YB6,B001AU8YBQ,B001AU8YCK,B001AUCK2U,B001AUEMFS,B001AUCK52,B001AU6XE6,B001AUEMH6';
amzn_wdgt.keywords='<?php echo $keyword; ?>';
amzn_wdgt.title='<?php echo $keyword; ?>';
amzn_wdgt.width='250';
amzn_wdgt.height='250';
amzn_wdgt.shuffleTracks='True';
amzn_wdgt.marketPlace='US';
</script>
<script type='text/javascript' src='http://wms.assoc-amazon.com/20070822/US ... ect_1_5.js'>
</script>
Any help is appreciated. Thanks!