简体   繁体   中英

Javascript code, unterminated string literal with included javascript

This script works fine, unless the included file contains javascript, then it breaks with the "unterminated string" literal error. Removing spaces and linebreaks does not cure the problem.

<script type="text/javascript">
var myArray = [
'url',
'url2',
'url3',
'url4',
'url5',
];
var i, numDomains = myArray.length, found = false;
for (i = 0; i < numDomains; i++) {
if (document.referrer.indexOf(myArray[i]) > -1) {
found = true;
}
}
document.write((found ? '<?php include("file1.php"); ?>' : '<?php include("file2.php"); ?>'));
</script>

A good solution would be where the included file can be any normal html file.

This is the output of the offending file:

<!-- google_ad_client = "pub-0705348955426556";/* ORG 468x60 */ google_ad_slot =     "2106718798";google_ad_width = 468;google_ad_height = 60;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><script src="http://googleads.g.doubleclick.net/pagead/test_domain.js"></scri

If file1.php or file2.php output anything with un-escaped ' characters or line breaks, this would cause the message you're getting.

Try removing all line breaks from the offending file, its output looks like this:

 <!-- google_ad_client = "pub-0705348955426556";/* ORG 468x60 */ google_ad_slot = "2106718798";google_ad_width = 468;google_ad_height = 60; --><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script<script src="http://googleads.g.doubleclick.net/pagead/test_domain.js">

Remove last comma from this line:

var myArray = [ 'url', 'url2', 'url3', 'url4', 'url5', ];
/*                                                   ^ */

If there are line breaks in the include files, that causes error. For example:

var a = "abc
adada"; // may cause error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM