简体   繁体   English

未捕获到的SyntaxError:意外的令牌/

[英]Uncaught SyntaxError: Unexpected token /

I just moved a bunch of javascript from being inline, into a separate js file, to take advantage of caching files etc. 我只是将一堆JavaScript从内联转移到一个单独的js文件中,以利用缓存文件等优势。

When I load my page that consumes the js file, I get the following error message: 加载使用js文件的页面时,收到以下错误消息:

Uncaught SyntaxError: Unexpected token / 未捕获到的SyntaxError:意外的令牌/

The line number it's complaining about looks like this: 它抱怨的行号如下所示:

url:'<?php echo site_url('switches/showknownvins/'.$Name.'/'.$model.'/'.$fn);?>',

It's a part of an ajax call. 这是ajax调用的一部分。 The lines of code above and below look like this: 上下的代码行如下所示:

    $.ajax({
    url:'<?php echo site_url('switches/showknownvins/'.$Name.'/'.$model.'/'.$fn);?>',
    type:'POST',
    dataType:'json',
    success: function(returnDataFromController) {

I have a total of 203 lines of js code, starting with : 我总共有203行js代码,从以下代码开始:

$(document).ready(function(){    

and ending with 并以

 });

When I paste the code back into the PHP file, it works fine. 当我将代码粘贴回PHP文件时,它可以正常工作。 I can't see where my error is. 我看不到我的错误在哪里。

Any suggestions? 有什么建议么?

Thanks 谢谢

EDIT 1 编辑1

If I rename my .js file to .php and include that, what's the impact? 如果将.js文件重命名为.php并将其包含在内,将会产生什么影响? Will the web server still cache it ? Web服务器还会缓存它吗? That's really what I'm after. 那真的是我所追求的。 I'm trying to improve the speed of my web application because I have a lot of mobile users. 我试图提高Web应用程序的速度,因为我有很多移动用户。

The JavaScript was inline because portions of it (notably here, the url value for the JavaScript AJAX call) was being set by PHP before being returned to the client. JavaScript之所以是内联的,是因为它的某些部分(特别是此处的JavaScript AJAX调用的url值)是由PHP设置的,然后才返回给客户端。 Hopefully you can see this from the paste of the problematic line in your question. 希望您可以从问题中出现问题的行中看到这一点。

Of course, when that PHP code gets to the user's browser, the browser won't understand it -- it's PHP code. 当然,当该PHP代码到达用户的浏览器时,浏览器将无法理解它-这是PHP代码。 Either keep the code you had inline as it was, or do the much harder thing and set up your server to serve dynamic JS if/when a requested static JS file isn't found. 如果没有找到请求的静态JS文件,则可以保持原来的代码内联,或者做更困难的事情,并设置服务器以提供动态JS。

By default, filenames ending in .js are not run through the PHP processor. 默认情况下,以.js结尾的文件名不会通过PHP处理器运行。

You can either reconfigure your webserver to do this. 您可以重新配置Web服务器来执行此操作。 Or rename your .js file to have a .php suffix. 或重命名您的.js文件以具有.php后缀。

Renaming it to .php should not affect caching, but you can send cache control headers to try to help the browser out. 将其重命名为.php不会影响缓存,但是您可以发送缓存控制标头来尝试帮助浏览器。

With CodeIgniter you can serve JS through a controller like this: 使用CodeIgniter,您可以通过这样的控制器提供JS服务:

Create a resources controller: 创建一个资源控制器:

/codeigniter/2.1.4/yourAppName/controllers/resources.php

In resources.php put this: resources.php以下内容:

class Resources extends CI_Controller
{
    public function js()
    {
        // JS call should look like the code below in your HTML
        // <script type="text/javascript" src="/resources/js/jsFileName.js"></script>
        // $this->uri->uri_string() should give the string "resources/js/jsFileName.js"
        if(is_file(APPPATH.'views/'.$this->uri->uri_string()))
        {
            header("Pragma: public");
            header("Cache-Control: maxage=604800"); // 1 week
            header('Expires: '.gmdate('D, d M Y H:i:s', (time()+604800)).' GMT'); // expire in 1 week
            header('Content-type: text/javascript');

            $this->load->view($this->uri->uri_string());
        }
    }
}

Create a JS view 创建一个JS视图

/codeigniter/2.1.4/yourAppName/views/resources/js/jsFileName.js

In jsFileName.js you can now use the full CodeIgniter library and native PHP: 现在,在jsFileName.js您可以使用完整的CodeIgniter库和本机PHP:

$(document).ready(function(){
    $.ajax({
        url:'<?php echo site_url('switches/showknownvins/'.$Name.'/'.$model.'/'.$fn);?>',
        type:'POST',
        dataType:'json',
        success: function(returnDataFromController){
        }
    });
});

It is worth noting that any autoloading that you declared in /config/autoload.php will be autoloaded upon every single JS call so depending on how heavy your APP is I would recommend forking a CI_Controller that only loads the bare necessities. 值得注意的是,您在/config/autoload.php声明的任何自动加载将在每个JS调用时自动加载,因此,根据您的APP的重量,我建议分叉仅加载基本必需品的CI_Controller。 Also if you are doing session stuff upon calling a JS file then things get REALLY ugly and unstable. 另外,如果您在调用JS文件时正在执行会话操作,那么事情将变得非常丑陋和不稳定。

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

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