Javascript 代码,包含 javascript 的未终止字符串文字

发布于 2024-09-16 23:46:47 字数 939 浏览 5 评论 0原文

该脚本工作正常,除非包含的文件包含 javascript,否则它会因“未终止的字符串”文字错误而中断。删除空格和换行符并不能解决问题。

<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>

一个好的解决方案是包含的文件可以是任何普通的 html 文件。

这是有问题的文件的输出:

<!-- 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生池水覆流年 2024-09-23 23:46:47

如果 file1.phpfile2.php 输出任何带有未转义的 ' 字符或换行符的内容,这将导致您收到消息。

尝试从有问题的文件中删除所有换行符,其输出如下所示:

 <!-- 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">

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">
缱倦旧时光 2024-09-23 23:46:47

删除该行的最后一个逗号:

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

Remove last comma from this line:

var myArray = [ 'url', 'url2', 'url3', 'url4', 'url5', ];
/*                                                   ^ */
Hello爱情风 2024-09-23 23:46:47

如果包含文件中有换行符,则会导致错误。例如:

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

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

var a = "abc
adada"; // may cause error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文