为什么我'm得到错误:“;SyntaxError:意外的标记<&”;
Why i'm getting error: "SyntaxError: Unexpected token <"?
这是我正在使用的代码:
<?php
$pattern="('.jpg$)|('.png$)|('.jpeg$)|('.gif$) |('.Gif$)"; //valid image extensions
$files = array();
$curimage=0;
if($handle = opendir($"http://newsxpressmedia.com/files/radar-simulation-files")) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
$files[] = $file;
$curimage++;
}
}
closedir($handle);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>change picture</title>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
#Timer_Countdown{
background:black;
color:yellow;
font-weight:bold;
text-align:center;
}
</style>
<script type = "text/javascript">
function displayNextImage() {
x = (x === images.length - 1) ? 0 : x + 1;
document.getElementById("img").src = images[x];
}
function displayPreviousImage() {
x = (x <= 0) ? images.length - 1 : x - 1;
document.getElementById("img").src = images[x];
}
var images = <?=json_encode($files)?>;
//var images = [];
var x = -1;
var swap_hours = 0;
var swap_minutes = 0;
var swap_seconds = 5;
var down_counter_hours;
var down_counter_minutes;
var down_counter_seconds;
function initTimer() {
down_counter_hours = swap_hours;
down_counter_minutes = swap_minutes;
down_counter_seconds = swap_seconds;
counter = setInterval(switcher, 1000);
}
function restartCounter() {
down_counter_hours = swap_hours;
down_counter_minutes = swap_minutes;
down_counter_seconds = swap_seconds;
}
function switcher() {
down_counter_seconds--;
if (down_counter_hours <= 0 && down_counter_minutes <= 0 && down_counter_seconds <= 0) {
swapColor();
restartCounter();
}
if (down_counter_seconds <= 0 && down_counter_minutes > 0) {
down_counter_seconds = 60;
down_counter_minutes--;
}
if (down_counter_minutes <= 0 && down_counter_hours > 0) {
down_counter_minutes = 60;
down_counter_hours--;
}
document.getElementById("Timer_Countdown").innerText = down_counter_hours+":"+down_counter_minutes+":"+down_counter_seconds;
}
function swapColor() {
displayNextImage();
}
</script>
<div id="div_hours" class="div_box"></div>
<div id="div_minutes" class="div_box"></div>
<div id="div_seconds" class="div_box"></div>
<div id="div_switcher" class="div_box"></div>
</head>
<body onload = "initTimer()">
<div id="Timer_Countdown"> </div>
<img id="img" src="http://newsxpressmedia.com/files/theme/radar000005.Gif">
<button onclick="displayPreviousImage(); restartCounter()">Previous</button>
<button onclick="displayNextImage(); restartCounter()">Next</button>
</body>
</html>
错误出现在线路上:
var images = <?=json_encode($files)?>;
如果我把这行改成这行:
var images = [];
然后代码运行良好,但没有使用php文件变量。线路出现问题:var images = <?=json_encode($files)?>;
我试图将此行更改为:echo json_encode($images);
或var images = echo json_encode($files);
,但出现了相同的错误。
我正在使用weebly来构建我的网站,我的网站服务器在ipage.com 上
如何修复错误?
看起来您没有启用短标签(并且使用的是5.4 php之前的版本),因此行<?=json_encode($files)?>
不会被php解析,只是直接发送。var images=<。。。不是有效的javascript表达式。以下是关于php的short_open_tag参数的一些信息:http://php.net/manual/en/ini.core.php#ini.short-打开标签
一些更改。更改
$pattern ="('.jpg$)|('.png$)|('.jpeg$)|('.gif$) |('.Gif$)";
至
$pattern = '/'.(png|jpg|jpeg|gif)$/i';
和
eregi($pattern, $file)
至
preg_match($pattern, $file)
看看会发生什么。eregi
被弃用为PHP 5.3.0。
您在PHP标记中使用'='。试试这个:
var images = <?json_encode($files)?>;
相关文章:
- "未捕获类型错误/丢弃意外“;因为一句台词而出现?(HTML w/<脚本>)
- "未捕获的语法错误:意外的标记<"当尝试使用谷歌'核心报告API
- 语法错误:"意外的标记<"在Web控制台中
- 未捕获的语法错误:意外的标记<在yii
- AJAX rest调用错误”;意外的令牌<&”;
- Jquery意外标记<
- 意外的令牌<当使用MEAN部署到iis服务器时
- Jquery DataTables服务器端语法错误:意外的标记<
- 未捕获的语法错误:意外的标记<在<!DOCTYPE html>
- error未捕获语法错误:意外的标记<
- Angular 2:SyntaxError:意外的标记<
- 未捕获的语法错误:意外的标记<并且没有错误404
- Gulp,未捕获语法错误:意外的标记<
- 获取错误SyntaxError:意外的令牌<djangojquery文件上传
- Webpack,React,JSX,Babel-意外的令牌<
- 意外的标记<来自样式标记
- 未捕获的语法错误:意外的标记<php(<?php)的列表1
- 意外的标记)<===
- Undercore模板问题-未捕获语法错误:意外的标记<
- 意外的令牌'<'-异步传输