4

I am trying to create my first Laravel site, but I can't figure out how to add a JS file to one of my pages.

I only want the JS file to be used on this page and no where else.

How do I get my page to load the JS file?

My directory structure is like so:

apply
    js
        myjsfile.js
    apply.blade.php

In my apply.blade.php

@extends('layouts.master')

@section('content')
<div class="row">
    <div id="applicationForm" class="col-md-9 col-xs-12">
    ...
    </div>
</div>

layouts.master file

<!DOCTYPE html>
<html>
    <head>
        @include('includes.head') 
    </head>

    <body>
        <div class="container">
        <div id="header">
            @include('includes.header')
        </div>
        @yield('content')
        <div id="footer">
            @include('includes.footer')
        </div>
        </div>
    </body>
</html>

1 Answer 1

13

your apply.blade.php

@extends('layouts.master')

@section('content')
<div class="row">
    <div id="applicationForm" class="col-md-9 col-xs-12">
    ...
    </div>
</div>
@stop

@section('myjsfile')
    <script src="js/myjsfile.js"><script>
@stop

your layouts.master

<!DOCTYPE html>
<html>
    <head>
        @include('includes.head') 
    </head>

    <body>
        <div class="container">
        <div id="header">
            @include('includes.header')
        </div>
        @yield('content')
        <div id="footer">
            @include('includes.footer')
        </div>
        </div>
    @yield('myjsfile')
    </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Where would I put the @ section bit? I want all my JS files to load in the footer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.