Pass data from a blade to the JS

0

I'm using Laravel and I have a blade with the variable:

{{$project->slug}}

On the blade I also have a button with which I want to open a new window, but as I have different projects it has to be 'dynamic'.

The code of the button is as follows:

<button type="button" class="btn btn-default btn-sm on buttonpreview" target="_blank" id="urlproyecto">
        <span class="glyphicon glyphicon-eye-open"></span>Ver Proyecto
</button>

The JS code is as follows:

$("#urlproyecto").click(function(){
    window.open('http://'+window.location.hostname+'/es/works/')
})

In the JS code you should add, at the end of the URL, the variable {{$project->slug}}

What would be the most efficient way to do it?

    
asked by Lluís Puig Ferrer 27.10.2017 в 13:00
source

2 answers

2
//Puedes crear una variable en javascript y asignarle la traida desde la controladora   
$("#urlproyecto").click(function(){
    var slug = '{{ $project->slug }}'; //En caso de que slug no sea una cadena pones = {{ project->slug }} sin las '
    window.open('http://'+window.location.hostname+'/es/works/'+slug);
});

//O puedes incrustarla exactamente donde necesites
$("#urlproyecto").click(function(){
    window.open('http://'+window.location.hostname+'/es/works/{{ $project->slug }}');
});
    
answered by 27.10.2017 в 14:52
0

What I've done is get the value of an input in the JS.

$("#urlproyecto").click(function(){
    var valorSlug = document.getElementById("slug").value;
    window.open('http://'+window.location.hostname+'/es/works/'+valorSlug)
})
    
answered by 27.10.2017 в 13:37