include javascript / jquery functions in backbone template

0

I have the following template with a table. An Array is traversed and a link is created in the row of the table

<tbody>
<%for(var i=0;i<resultados.length;i++){ %>
 <tr onclick="window.open('#/ensayo/<%- resultados[i].tipoEnsayo %>/<%- resultados[i].idEnsayo %>', '_blank')" class="selectableRow">
   <% if (mostrarResultados.mostrarTipoEnsayo == 1) { %><td><%- resultados[i].tipoEnsayo %></td><%}%>
   <% if (mostrarResultados.mostrarIdEnsayo == 1) { %><td><%- resultados[i].idEnsayo %></td><%}%>
 </tr>
<%}%>

If you click on the link, it will take you to another screen. The problem comes when the idEnsayo contains "%".

The URL that is formed is the following: http://localhost:8080/Petrocor/index_1.html#/ensayo/inmersion/EAutoclave_6%Mo-SS_1

Da error ( Uncaught URIError: URI malformed ) when forming the URL the encode of the character "%" is "% 25"

How can I escape this character when the URL is formed inside the html, with the underscore syntax?

In javascript it would be something like this:

var ensayo=resultados[i].idEnsayo;
if(ensayo.includes('%'){
ensayo=ensayo.replace('%','%25');
}
    
asked by miss Robot 06.10.2017 в 13:03
source

2 answers

1

If I do not understand the question wrong, you want to escape the resource identifier. You can do it with encodeURIComponent

let baseUrl='http://localhost:8080/Petrocor/index_1.html#/ensayo/inmersion/';
$('button').click(function () {
  let value=$('#texto').val();
  console.log('el texto es "${value}"');
  console.log('El texto listo para una URL es "${baseUrl}${encodeURIComponent(value)}"');
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="texto" value="EAutoclave_6%Mo-SS_1"/>
<button>Pulsa</button>
    
answered by 06.10.2017 / 13:53
source
0

Finally it would look like this:

<tbody>
  <%for(var i=0;i<resultados.length;i++){ %>
    <% var idEnsayo=resultados[i].idEnsayo;%>
    <tr onclick="window.open('#/ensayo/<%- resultados[i].tipoEnsayo %>/${encodeURIComponent(idEnsayo)}', '_blank')" class="selectableRow">
    
answered by 09.10.2017 в 12:36