Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError when using jQuery.html to insert HTML fragment containing script element [closed]

I am using jquery ajax request which gets the HTML content from backend code. If status is OK, I am appending HTML content to a div. I am using following code to append div using jquery ajax. as $('#divElm').html(response.data);. But after appending I am getting following error:

SyntaxError: missing } in compound statement

The HTML content rendering from backend (ie., response.data) contains the following which is causing this error:

<script type="text/javascript"> 
function init() {
if(d.getElementById('normal')){
    window.normalPop= d.getElementById('normal');
    //complicated is local 
    var complicated= normalPop.parentNode.removeChild(normal);
    d.getElementsByTagName('body')[0].appendChild(complicated);
    }
}   
addLoadEvent(init);
</script>

I am not sure why this error "SyntaxError: missing } in compound statement" is coming.The } braces are closing properly in the code.

If I am using innerHTML instead of html() it is working fine. No error is rendering.

like image 855
user521024 Avatar asked Nov 04 '22 05:11

user521024


1 Answers

The.html() uses .append() which inturn uses domManip() which does the script sniffing and execution. Because of this reason, the script code in coming from backend executed and caused this error "SyntaxError: missing } in compound statement". I investigated through the script code, the comment lines (//) in script code make commented some JS code too.So, after changing single line (//) to multi-line comments(/** **/).It get fixed.Thanks All for your time.

like image 139
user521024 Avatar answered Nov 15 '22 00:11

user521024