开发者

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("&lt;!-- xxxxxxx --&gt;
******index.php:11 Uncaught SyntaxError: Unexpected token ILLEGAL******
            &lt;script type=&quot;text/javascript&quot;&gt;
            xxxxxxx_bid = &quot;xxxxxxxxxxx&quot;;
            &lt;/script&gt;
            &lt;script type=&quot;text/javascript&quot; src=&quot;http://xxx.xxxxxx.com/k.js&quot;&gt;&lt;/script&gt;
            &lt;!-- xxxxxxxx --&gt;").text();    })开发者_运维技巧;
</script>

OR

PHP/jQuery Uncaught SyntaxError: Unexpected token ILLEGAL

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜