代码改变世界

laravel加载javascript库

  youxin  阅读(988)  评论(0编辑  收藏  举报

一篇文章:

Generating a Link to a Javascript File

Problem

You want your Blade template to load an external javascript file.

 

Instead of using <script ...> directly, you want to do this with the HTML facade.

 

Solution

Use the HTML::script() method.

 

Just pass the path to the javascript file.

{{ HTML::script('js/functions.js') }}

This produces the following HTML code.

<script src="http://your.url/js/functions.js"></script>

If the file path you pass isn't a complete URL, Laravel will use your application's URL to build a complete URL.

You can pass additional attributes in an array as the second argument.

{{ HTML::script('js/functions.js', array('async' => 'async')) }}

The attributes will be added to the script tag as the result below illustrates.

<script async="async" src="http://your.url/js/functions.js"></script>
 

Discussion

The type attribute of <script> tags is optional with HTML5.

But if your web page is still HTML 4.01, you should add the "type" => "text/javascript" to the attributes you pass this method.

转自:http://laravel-recipes.com/recipes/183

写在前面的话:

1.前提是需要使用blade模板引擎

2.css js image 文件夹建在laravel 的 public 目录下面

3.生成的路径默认都是相对路径

 

A: 加载css文件 (用下面的格式把正常的link替换掉)

{{ HTML::style('css/custom.css') }}

在页面里生成的样子如下

<link media="all" type="text/css" rel="stylesheet" href="http://local.lv.com/css/custom.css">

B: 加载js文件 (用下面的格式把正常的script替换掉)

{{ HTML::script('js/custom.js') }}

在页面里生成的样子如下

<script src="http://local.lv.com/js/custom.js"></script>

C: 页面里加图片(同样去掉img标签)

{{HTML::image('images/hot_1.gif')}}

生成的样子

<img src="http://local.lv.com/images/c_4.gif">

 

 

如果想自定义img的话(加入alt或是title之类的属性),那就什么都不用改,直接使用<img src="images/c_4.gif" /> 就可以了,前提是需要配置apache,把DocumentRoot 直接设置到public目录下,因为使用的都是相对路径.

转自:http://www.cnblogs.com/debmzhang/p/3500429.html

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示