在“bottom”处加载CakePHP布局中的Javascript文件 [英] Load Javascript files in CakePHP Layout at "bottom"
问题描述
目前,我有一个大的CakePHP应用程序的布局和很多的意见。在布局中,我加载了大多数视图所需的头文件中的Javascript文件。在视图中,我自己,我加载额外的Javascript文件(例如,加载只需要那里的库文件),或者我添加一些Javascript代码,只在脚本标记中与此视图相关,例如,如果我需要一个点击处理程序。
Currently, I have a big CakePHP application with a layout and a lot of views. In the layout, I load Javascript files in the head which are needed by most views. In the views themself, I either load additional Javascript files (e.g., load a library file that is only needed there), or I add some Javascript code that is only relevant for this view in a script tag, for example if I need a click handler.
因为一个已知的事实,即在HTML头部加载Javascript文件阻止加载页面,我想把它们放在底部,然后关闭body标签。但是如果我这样做,我的视图中加载内容的Javascript打断,因为它不知道我加载的Javascript文件。我理解加载视图中的Javascript代码在加载文件之前执行。但是我该如何防止呢?
Since it is a known fact that loading Javascript files in the HTML head blocks loading the page, I wanted to put them at the bottom before closing the body Tag. But if I do so, the Javascript in my views that load the content breaks because it does not know about my loaded Javascript files. I understand that the Javascript code in the loaded views is executed before my files are loaded. But how can I prevent that?
我目前正在使用布局(和其他地方)中的HTML助手加载我的JS文件,如下所示:
I'm currently using the HTML Helper in the Layout (and everywhere else) to load my JS files like this:
<?php echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'); ?>
我使用JS Helper在页面末尾输出JS。
And I use the JS Helper to "output" JS at the end of the page with
<?php echo $this->Js->writeBuffer(); ?>
有没有办法我可以在视图中追加我的JS代码,调用writeBuffer?这能帮助我吗?
Is there a way I can append my JS code in the views, so that it is executed when I call writeBuffer? Can that help me out?
推荐答案
这是我在我看来:
echo $this->Html->script('filename', array('inline' => false));
这是我在我的布局底部做的:
And this is what I do at the bottom of my layout:
echo $this->fetch('script');
这篇关于在“bottom”处加载CakePHP布局中的Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!