简体   繁体   English

为什么我会收到错误消息:“ SyntaxError:意外令牌<”?

[英]Why i'm getting error: “SyntaxError: Unexpected token <”?

This is the code i'm using: 这是我正在使用的代码:

<?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">&nbsp;</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>

The error is on the line: 错误就在网上:

var images = <?=json_encode($files)?>;

If i change this line to this line: 如果我将此行更改为此行:

var images = [];

Then the code is working fine but without using the php files variable. 然后,代码可以正常工作,但不使用php files变量。 Something is wrong with the line: var images = <?=json_encode($files)?>; 该行有问题: var images = <?=json_encode($files)?>;

I tried to change this line to: echo json_encode($images); 我试图将此行更改为: echo json_encode($images); or to var images = echo json_encode($files); var images = echo json_encode($files); but same error. 但同样的错误。

I'm using weebly to build my site and my site server is on ipage.com 我正在使用weebly来构建网站,并且我的网站服务器在ipage.com上

How can i fix the error ? 我该如何解决错误?

It looks like you don't have short tags enabled(and are using a pre 5.4 php version), thus the line <?=json_encode($files)?> is not parsed by php and is just send straight through. 似乎您没有启用短标签(并且正在使用5.4之前的PHP版本),因此php无法解析<?=json_encode($files)?> ,而是直接发送。 var images = <... is not a valid javascript expression. var images = <...不是有效的JavaScript表达式。 Here's some information about php's short_open_tag parameter: http://php.net/manual/en/ini.core.php#ini.short-open-tag 以下是有关php的short_open_tag参数的一些信息: http : //php.net/manual/zh/ini.core.php#ini.short-open-tag

A couple of changes. 进行了一些更改。 Change 更改

$pattern ="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$) |(\.Gif$)";

to

$pattern = '/\.(png|jpg|jpeg|gif)$/i';

and

eregi($pattern, $file)

to

preg_match($pattern, $file)

and see what happens. 看看会发生什么。 eregi deprecated as PHP 5.3.0. eregi不推荐使用PHP 5.3.0。

You are using '=' inside the PHP tags. 您在PHP标记内使用'='。 Try this: 尝试这个:

var images = <?json_encode($files)?>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:我收到错误“Uncaught SyntaxError:Unexpected token {” - Javascript: I'm getting the error “Uncaught SyntaxError: Unexpected token { ” 我收到了Uncaught SyntaxError:嵌入:意外的令牌 - I'm getting Uncaught SyntaxError: embedded: Unexpected token 为什么我会收到 Uncaught SyntaxError:意外令牌:标识符 - Why am i getting Uncaught SyntaxError: unexpected token: identifier 为什么我得到:未捕获的SyntaxError:此处出现意外的令牌ILLEGAL - Why am I getting : Uncaught SyntaxError: Unexpected token ILLEGAL here 为什么我在 JSON 文件中收到错误消息“SyntaxError: unexpected token: ':'”? - Why am I getting a error saying “SyntaxError: unexpected token: ':'” in a JSON file? 收到 `Uncaught SyntaxError: Unexpected token &lt;` 的错误 - Getting error of `Uncaught SyntaxError: Unexpected token <` 在脚本“Uncaught SyntaxError: Unexpected token )”中得到错误 - Getting the error in Script "Uncaught SyntaxError: Unexpected token )" 获取语​​法错误:意外的令牌 - Getting syntaxError: Unexpected token < 为什么测试失败并出现错误 SyntaxError: Unexpected token {? - Why tests fail with an error SyntaxError: Unexpected token {? 我在调用 openweathermap 时收到 SyntaxError: Unexpected token &lt; in JSON at position 0 - I'm getting SyntaxError: Unexpected token < in JSON at position 0 while calling openweathermap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM