3

Can anyone please guide me regarding how can I add custom javascript in Laravel project?

I did google and found some steps but unable to figure out what the error is going on?

Steps I

  1. Created a custom.js in my public folder with a simple function
  2. add script tag in my app.blade.php
  3. extends my layout

But the function is not working

$(document).ready(function()
{
    alert("hello this is test");
})

custom.js

enter code here

<script type="text/javascrip" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>"
<script type="text/javascript" src="{{asset('js/custom.js')}}"></script>

code I added in my main file app.blade

but when load my page it doesn't give me an alert. Alert is just for testing I tried with various function.

4
  • Have you tried viewing the source code from the web browser? Commented Sep 24, 2019 at 10:41
  • First of all thanks chusky for showing interest but yes I did and its showing but nothing working and one more thing If I am making a simple function without $document.ready like function check(){alert("example") and call it via on click function its working but when i use it by $(document).ready(function(){}) its not working Commented Sep 24, 2019 at 10:43
  • 1
    @SamirParuthi Hello, Can you check your browser console? If any error occurred. please shared with us. Commented Sep 24, 2019 at 10:48
  • What error are you getting? Commented Sep 24, 2019 at 10:52

2 Answers 2

3
  1. You misspelled javascript
  2. Remove the (") after writing the </script>

For Javascript:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="{{ URL::asset('js/custom.js') }}"></script>

Make your directory structure is like this for JS: /public/js/custom.js

Sign up to request clarification or add additional context in comments.

Comments

0

I have to review your code. I have found an error in your code like a spelling mistake.

<script type="text/javascript" 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>"

You have miss t at the end of javascript.

2 Comments

@SamirParuthi When use $(document).ready(function() you need to load jquery.min.js But wrong spelling privent load your js file.
@SamirParuthi Happy that I can solve your issue. please accept answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.