Problem with window.open () JS

0

I have a table composed of 3 columns: link, status, action.

In the action column, I have 2 links that will serve as "buttons": VIEW & TO DISCARD

Thistablehasinaverage+100lines...

IneedtheuserclickonVIEW,togetthevalueofthecolumn"Link" in the respective line ... so that a link is created where a redirect to a page will occur in a new tab.

Logical example:

String value = takes the value of the link column in the respective row
String link=" link " + value

What I've done:

I'm using the following function

<script type="text/javascript">
$(document).ready(function() {
    $('#mytable td').click(function() {
        posicao = $(this).parent().children().index(this);
        link = $('td:first', $(this).parents('tr')).text();

        $(this).attr('title', link);

        window.open("http://www.webmotors.com.br"+link);
    });
});
</script>

This function takes the value of the link column when I click on the row (tr) of the table ... I wanted to get the value only when I clicked SEE, but I could not do it this way, if someone knows a way to do this I'll be grateful.

The problem:

When you run the window.open (site)
The page that generates in a new tab comes with a "% 20" between the site and the link that I have in the table value ...

This%20preventsopeningthepage...ifItakeitinhandthepageopens...Idonotunderstandwhyheshowsupinthemiddle...Ineedtogethimout.

Thepartthatmountsthelinkis:

window.open("http://www.webmotors.com.br"+link);

I need help with this.

    
asked by anonymous 23.06.2015 / 20:50

3 answers

3

%20 is ASCII Encode equivalent to space , that is, a simple ' ' (ignores quotation marks)

Your link probably has a space at the beginning, use the .trim() method to remove unwanted spaces at the beginning and end.

Example:

window.open("http://www.webmotors.com.br"+link.trim());
    
23.06.2015 / 21:16
2

The Maicon solution is correct, but I do not think it's a good idea to "recycle" pieces of the UI to use in redirects, AJAX, etc. because these kinds of problems happen that you have encountered.

What you can do is put one more attribute in your <tr> , something like

<tr data-link="/comprar/toyota/…">…</tr>

And then you would access the link via

var link = $(this).parents('tr').data('link');

(Consider even putting http://www.webmotors.com.br into data-link also; on the day you want to have more than one site, you only need to move one place.)

    
23.06.2015 / 21:21
-1

use window.location.href , and place a slash after the link, '

window.location.href("http://www.webmotors.com.br/"+link);
    
23.06.2015 / 20:57