最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: is not defined - Stack Overflow

programmeradmin1浏览0评论

After I deploy my project on Heroku, I get for the following code in Firebug this error:

ReferenceError: SirTrevor is not defined

<script type="text/javascript" charset="utf-8">
$(function(){
  var instances = $('.sir-trevor-area'),
  l = instances.length;
while (l>=2) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Video"], defaultType: "Video" });
 l--;
};
while (l>=1) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Embedlylink"], defaultType: "Embedlylink" });
 l--;
};
while (l>=0) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Heading", "Text", "Image", "Video", "Embedly"], defaultType: "Text" });
 l--;
};
});
</script> 

Firebug highlights this line "

new SirTrevor.Editor({ el: instance, blockTypes: ["Heading", "Text", "Image", "Video", "Embedly"], defaultType: "Text" });

"

On localhost everything seems fine, no errors. Anyone an idea what the problem could be? Thank you so much for your help! Really appreciated!

UPDATE:

SirTrevor is defined in sir-trevor.js which is successfully loaded via the Rails asset pipeline in the application.js file.

This is my application.js file:

//= require jquery
//= require jquery_ujs
//= require sir-trevor
//= require underscore.js
//= require eventable.js
//= require twitter/bootstrap
//= require jquery.sidr.js
//= require_tree .

Before push to Heroku I do "rake assets:prepile" . On localhost everything seems fine.

The application.html.erb header:

<!DOCTYPE html>
<html>
<head>
  <title>Tt</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <link href="//netdna.bootstrapcdn/font-awesome/4.0.0/css/font-awesome.css" rel="stylesheet">
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>

</head>

After I deploy my project on Heroku, I get for the following code in Firebug this error:

ReferenceError: SirTrevor is not defined

<script type="text/javascript" charset="utf-8">
$(function(){
  var instances = $('.sir-trevor-area'),
  l = instances.length;
while (l>=2) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Video"], defaultType: "Video" });
 l--;
};
while (l>=1) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Embedlylink"], defaultType: "Embedlylink" });
 l--;
};
while (l>=0) {
 instance = $(instances[l]);
 new SirTrevor.Editor({ el: instance, blockTypes: ["Heading", "Text", "Image", "Video", "Embedly"], defaultType: "Text" });
 l--;
};
});
</script> 

Firebug highlights this line "

new SirTrevor.Editor({ el: instance, blockTypes: ["Heading", "Text", "Image", "Video", "Embedly"], defaultType: "Text" });

"

On localhost everything seems fine, no errors. Anyone an idea what the problem could be? Thank you so much for your help! Really appreciated!

UPDATE:

SirTrevor is defined in sir-trevor.js which is successfully loaded via the Rails asset pipeline in the application.js file.

This is my application.js file:

//= require jquery
//= require jquery_ujs
//= require sir-trevor
//= require underscore.js
//= require eventable.js
//= require twitter/bootstrap
//= require jquery.sidr.js
//= require_tree .

Before push to Heroku I do "rake assets:prepile" . On localhost everything seems fine.

The application.html.erb header:

<!DOCTYPE html>
<html>
<head>
  <title>Tt</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <link href="//netdna.bootstrapcdn./font-awesome/4.0.0/css/font-awesome.css" rel="stylesheet">
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>

</head>
Share Improve this question edited Nov 21, 2013 at 0:07 YvonC asked Nov 20, 2013 at 20:52 YvonCYvonC 3492 gold badges6 silver badges23 bronze badges 15
  • 1 Well, SirTrevor is not defined. Start there. 1) from where is SirTrevor to be defined and; 2) is the resource being successfully loaded? (Use web-browser developer tools to monitor the network requests.) – user2864740 Commented Nov 20, 2013 at 20:55
  • Check that all your Javascript files are loading properly. – user1864610 Commented Nov 20, 2013 at 20:55
  • The script containing the SirTrevor class isn't loading. – adeneo Commented Nov 20, 2013 at 20:55
  • Learn how to debug JavaScript. – Felix Kling Commented Nov 20, 2013 at 21:06
  • I guess it's a external script and you didn't prepile it.See in the console if it's not loaded. – Zippie Commented Nov 20, 2013 at 21:08
 |  Show 10 more ments

1 Answer 1

Reset to default 2

In order to close the question a short roundup: Thanks to the help of @iamjpg I finally figured it out.

First I eliminated all the javascript from the <body> and put into separate js files. Then I adjusted the hierarchy in the application.js file. Sir-Trevor has dependencies to underscore.js and eventable.js, so they have to be higher than sir-trevor.js .

//= require jquery
//= require underscore.js
//= require eventable.js
//= require twitter/bootstrap
//= require jquery_ujs
//= require sir-trevor
//= require jquery.sidr.js
//= require_tree .
发布评论

评论列表(0)

  1. 暂无评论