PHP/jQuery Uncaught SyntaxError: Unexpected token ILLEGAL
i have problem with between php and jQuery/Javascript...It wont show up or can't work.
My Code:
if($bottom_1_banner == "true"){
//$data_AD .= '$(\'.ban_bottom\').html(\''.htmlentities($bottom_banner).'\').text();';
$data_AD .= '$(\'.ban_bottom\').html("'.htmlspecialchars($bottom_banner).'").text();';
}
Error Log: (Chrome/Safari)
<script type="text/javascript">
$(document).ready(function() {
$('.ban_bottom').html("<!-- xxxxxxx -->
******index.php:11 Uncaught SyntaxError: Unexpected token ILLEGAL******
<script type="text/javascript">
xxxxxxx_bid = "xxxxxxxxxxx";
</script>
<script type="text/javascript" src="http://xxx.xxxxxx.com/k.js"></script>
<!-- xxxxxxxx -->").text(); })开发者_运维技巧;
</script>
OR
Edited: Converted Image to Text.
I had the same problem with the new line in JavaScript. If you are getting the value from MySql you can try this:
$php_string = str_replace('<br />','<br />\\',nl2br($php_string));
First replace the line-break with an html <br/>
tag and then add the \ after the generated <br/>
tag; So the JS line always ends with a \, that indicates a correct new line in the JavaScript code.
Based by your code above, one thing for sure, you need to define $data_AD
variable before using .=
or just use =
without .
Maybe do
$data_AD .= '$(\'.ban_bottom\').html("'.$bottom_banner.'").text();';
...
<?php echo str_replace('\n', '\\n', $data_AD); ?>
to remove the line breaks?
Get rid of the new lines in the HTML. Javascript doesn't understand strings on multiple lines. You can alternatively replace the \n with \ \n:
This does not work:
var a = "This is a
string for me";
This works
var a = "This is a \
string for me";
or
var a = "This is a string for me";
This line of code works flawless here. I'd guess its an file encoding issue with the .php-file. Check for bad characters.
精彩评论