views:

168

answers:

3

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"&gt;
</script><script src="http://googleads.g.doubleclick.net/pagead/test_domain.js"&gt;&lt;/scri
+1  A: 

Remove last comma from this line:

var myArray = [ 'url', 'url2', 'url3', 'url4', 'url5', ];
/*                                                   ^ */
Vadim Shender
Although he should do this, it isn't the cause of the error
Michael Robinson
Thanks for cutting my lunch. Just kidding :) I didn't post my comment as an answer since he mentions that when the .php file contains javascript, THAT's when the script breaks.. so I figured it's not related to the array.
Marko
Thanks for the tip, it still breaks, even with the redundant comma removed.
Rhys
+3  A: 

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"&gt;&lt;/script&lt;script src="http://googleads.g.doubleclick.net/pagead/test_domain.js"&gt;
Michael Robinson
include() does not return the evaluated code of the file, so addslashes is out of question. unless you are talking about using it with output control functions ob_*()
w35l3y
A: 

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

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