Insert JavaScript variable to a Form action. with pug / jade

0

I need to insert the value of a javascript variable at action of a form

The Script is this:

    var url = window.location;
    var urlcomplete = url.href;
    var urlparts = urlcomplete.split("/");
    var years = urlparts[urlparts.length - 1];
    var route = "/app/imagenes/"+years; //variable a pasar 

the form is this:

    form(action="//AQUI QUIERO INSERTAR EL VALOR DE LA VARIABLE//" method="POST" enctype="multipart/form-data")
    
asked by Michael Valdes Gonzalez 02.02.2018 в 17:57
source

1 answer

2

Inside the pug file, the variables must go with a script in front, in such a way that:

- var url = window.location;
- var urlcomplete = url.href;
- var urlparts = urlcomplete.split("/");
- var years = urlparts[urlparts.length - 1]; 
- var route = "/app/imagenes/"+years; //variable a pasar 

form(action=route method="POST" enctype="multipart/form-data")

You have the example: link

Edited

In this case, location.href will not find it. To change the href of the formula you have to resort to create a javascript code. In jade, to generate a script the label script. is used (note the end point), in this way what is tabulated inside will be javascript code.

We can use this to create the formula and then set the action by means of javascript:

form(id="formr" method="POST" enctype="multipart/form-data")
script.  
   var url = window.location;
   var urlcomplete = url.href;
   var urlparts = urlcomplete.split("/");
   var years = urlparts[urlparts.length - 1]; 
   var route = "/app/imagenes/"+years; //variable a pasar 

   document.getElementeById('formr').action = route

Since location.href is an environment variable, it is the browser that deals with it. Do not jade

    
answered by 03.02.2018 / 20:24
source