admin管理员组

文章数量:1277555

I just moved a bunch of javascript from being inline, into a separate js file, to take advantage of caching files etc.

When I load my page that consumes the js file, I get the following error message:

Uncaught SyntaxError: Unexpected token /

The line number it's plaining about looks like this:

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

It's a part of an ajax call. 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 :

$(document).ready(function(){    

and ending with

 });

When I paste the code back into the PHP file, it works fine. I can't see where my error is.

Any suggestions?

Thanks

EDIT 1

If I rename my .js file to .php and include that, what's the impact? Will the web server still cache it ? 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.

I just moved a bunch of javascript from being inline, into a separate js file, to take advantage of caching files etc.

When I load my page that consumes the js file, I get the following error message:

Uncaught SyntaxError: Unexpected token /

The line number it's plaining about looks like this:

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

It's a part of an ajax call. 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 :

$(document).ready(function(){    

and ending with

 });

When I paste the code back into the PHP file, it works fine. I can't see where my error is.

Any suggestions?

Thanks

EDIT 1

If I rename my .js file to .php and include that, what's the impact? Will the web server still cache it ? 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.

Share Improve this question edited Feb 6, 2014 at 18:21 dot asked Feb 6, 2014 at 18:14 dotdot 15.7k43 gold badges128 silver badges260 bronze badges 14
  • 8 You're trying to do PHP stuff in your JavaScript file. – Pointy Commented Feb 6, 2014 at 18:15
  • 2 php isn't interpreted in .js files. – Jason P Commented Feb 6, 2014 at 18:15
  • 2 Did you set up your web server to parse JS files with PHP before sending them to the web browser? – MonkeyZeus Commented Feb 6, 2014 at 18:17
  • Looks like CodeIgniter. Are you using CodeIgniter to serve the JS files through a PHP controller? – MonkeyZeus Commented Feb 6, 2014 at 18:18
  • 1 It sounds like you took JavaScript code, portions of which are generated by PHP, and copied it into a .js file. That, of course, will not work. Anything PHP-related has to be in a PHP file (which is probably why it was inline in the first place). Look at the value of your url: -- it's set to the result of a PHP function called site_url. – Madbreaks Commented Feb 6, 2014 at 18:26
 |  Show 9 more ments

3 Answers 3

Reset to default 3

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

By default, filenames ending in .js are not run through the PHP processor.

You can either reconfigure your webserver to do this. Or rename your .js file to have a .php suffix.

Renaming it to .php should not affect caching, but you can send cache control headers to try to help the browser out.

With CodeIgniter you can serve JS through a controller like this:

Create a resources controller:

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

In resources.php put this:

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

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

In jsFileName.js you can now use the full CodeIgniter library and native 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 remend forking a CI_Controller that only loads the bare necessities. Also if you are doing session stuff upon calling a JS file then things get REALLY ugly and unstable.

本文标签: javascriptUncaught SyntaxError Unexpected token Stack Overflow