Home > other >  How to get asset location in laravel
How to get asset location in laravel

Time:05-09

I have a js file in my laravel project and i have some links in it that points to some asset files. I am trying to locate this dynamically. How do i do this??

var textarea = document.getElementById('editor');
sceditor.create(textarea, {
    emoticons:{
    dropdown: {
        ':)': '../editor/emoticons/smile.png',
        ':angel:': '../editor/emoticons/angel.png',
        ':alien:': '../editor/emoticons/alien.png',
        ':blink:': '../editor/emoticons/blink.png',
        ':angry': '../editor/emoticons/angry.png',
        ':D': '../editor/emoticons/grin.png',
        ':P': '../editor/emoticons/tongue.png',
        ':blush:': '../editor/emoticons/blush.png',
        ':(': '../editor/emoticons/cwy.png',
        '<3': '../editor/emoticons/heart.png'
    }
},
    format: 'xhtml',
    icons: 'monocons',
    style: '../editor/minified/themes/content/default.min.css'
});

I have a folder in my public folder that contains all this but how do i locate them dynamically. I know if it is a normal blade file i can use {{ asset('') }} how do i do this in this js file?

CodePudding user response:

I hope in laravel blade you can directly use blade syntax inside a javascript code like this try it out like this

var textarea = document.getElementById('editor');
sceditor.create(textarea, {
    emoticons:{
    dropdown: {
        ':)': '{{asset('editor/emoticons/smile.png') }} ',
    }
},
    format: 'xhtml',
    icons: 'monocons',
    style: '{{ asset(' editor/minified/themes/content/default.min.css') }} '
});


I hope it works for you

CodePudding user response:

In the head of your HTML (in a blade file), you can use window.assetUrl = '{{asset('')}}';. Then, in your JS files, you can call ':)': window.assetUrl 'editor/emoticons/smile.png'. This way your asset URL is dynamic (based on what the app URL is).

  • Related