I have problem when I want to use $javascript->link('prototype')
in the default开发者_开发百科.ctp
layout. It returns:
Undefined variable: javascript [APP\views\layouts\default.ctp, line 6]
I also added this code into app_controller.php:
<?
class AppController extends Controller {
var $Helpers = array('Html','Javascript','Ajax','Form');
}
?>
The file prototype.js
is already in webroot/js
folder.
Where is the problem?
I have had this problem many times. It's usually either caused by the controller code being overwritten somewhere or some weirdness happening with Cake's automagic stuff. If you remove all of your helpers and then add them one by one it will probably work eventually.
Another perfectly valid way of generating JavaScript links is by using the following which doesn't access the $javascript variable:
echo $html->script(array('prototype'));
It has to be $helpers
instead of $Helpers
.
You just open the error console of the Firefox browser (shortcut key ctrl+shift+j).
- Find the error and click on it.
- After clicking, you will see the head portion.
- Note the location of the JavaScript file (*.js) which you want to locate (you will see the location is not correct).
- Cut the JavaScript file from webroot and paste it in given location of head block.
Example:
This will show on error console. Map_demo is my project, and in its place your project name will display:
<script type="text/javascript" src="/map_demo/js/test.js"></script>
- Cut the JavaScript file from webroot
- Make the JavaScript folder in your project application folder,
/map_demo/js
- Paste
test.js
(your script file) here
Now your JavaScript function will work.
Just in case somebody else comes across this bug/issue: it was also happening to me, until I commented out the line $session->flash();
in my default
layout. Realising that the error was being caused by flash messages, I went back to the controller and noticed that I was using separate layouts for flash messages (e.g. 'message_alert') and that those layouts didn't actually exist in the view folder!
So remember, errors like this could mean that a file is not defined yet. Best of luck.
精彩评论