Laravel blade use $(document).ready function

前端 未结 2 1389
失恋的感觉
失恋的感觉 2021-01-13 14:32

I\'m trying to use laravel blade templates including some javascript code into child view.

I have my mail app.blade.php file, where is placed string

相关标签:
2条回答
  • 2021-01-13 14:59

    Please follow this way

    for jquery script file do this

    <script src="{!!url('/js/jquery.min.js')!!}"></script>
    

    and do this for content section

    @extends('layouts.app')
    @section ('content')
    Settings main page
    
    <script type="text/javascript">
        $(document).ready(function() {
            alert("Settings page was loaded");
        });
    </script>
    
    @endsection
    

    Updated


    Rather to scripting in content section, it would much better and professional way to create another section for scripting in app.blade.php

    normally I follow this way

    <html>
      <head> 
        @yield('page-style-files')
      </head>
    
      <body>
        <div class="wrapper">
         @yield('content')
        </div>
      </body>
    
      @yield('page-js-files')
      @yield('page-js-script')
    
    <html>
    

    so for example your code will look like this

    @extends('layouts.app')
    @section ('content')
      Settings main page    
    @endsection
    
    @section('page-style-files')
     <link href="....css" />
    @stop
    
    
    @section('page-js-files')
     <script src=".....js"></script>
    @stop
    
    @section('page-js-script')
    <script type="text/javascript">
        $(document).ready(function() {
            alert("Settings page was loaded");
        });
    </script>
    @stop
    
    0 讨论(0)
  • 2021-01-13 15:16

    I had this same problem which seemed more to do with the js files being loaded in with the defer attribute.

    I was getting the same console warning about $ not being defined.

    So I tested this notion with...

    <script type="text/javascript">
        setTimeout(function () {
            $(document).ready(function(){
                alert('now');
            });
        }, 5000);
    </script>
    

    Which then worked, so I assume it's to do with defer (using Firefox Developer Edition on Linux Mint).

    Anyway, a good reason to put all my scripts into proper js files ;-)

    0 讨论(0)
提交回复
热议问题