0

I have action button for editing that doesn't have option "Open in new tab" when i right click. Here is my code:

function actionsTemplateConversion(data) {
    if (data != null){
        if (canEdit){
                row += '<a id="edit" title="${editTr}" onclick="edit(event);"></a>';
            }
            return row;
        }
    }

function edit(event) {
    table = $('#table').data();
    var dataItem = table.dataItem($(event.currentTarget).closest("tr"));

    window.location = "users/"+dataItem.id+"/editUser.html?id="+"${id}"+"&name="+"${name}";
    }

I've tried use href instead onclick and put this link from edit() function in href and with that i get option Open in new tab for right click, but also there is problem with this because if there is single qoutes in name than everything meesses up because of that qoutes. So only way is with this function. I also tried with this window.location.href but it doesnt work. Is there any other option for opening link beside window.location?

alonso05
  • 129
  • 3
  • 15
  • Possible duplicate of [Chrome, Javascript, window.open in new tab](https://stackoverflow.com/questions/15818892/chrome-javascript-window-open-in-new-tab) – G07cha Jun 13 '17 at 15:37
  • I saw this, but it doesn't help me because this solution always open link in new tab. I need this option so that user can choose where he is edit data – alonso05 Jun 13 '17 at 15:40

2 Answers2

0

you can target at anchor tag this cause browser open new tab <a target="_blank" href=""/>

OR

if(user)
 window.open(encodeURI('url'), '_blank');
else
window.open(encodeURI('url'));

if the quote in name try to encode it https://www.w3schools.com/TAGS/ref_urlencode.asp

user 13
  • 134
  • 8
0
<a id="edit" title="${editTr}" onclick="edit(event);"></a>

That isn't a link. A link must have an href attribute. Without one, there is no URL to open when you click it or open in new tab. There is just a JavaScript program that runs.

I've tried use href instead onclick

That's the solution

link from edit() function in href and with that i get option Open in new tab for right click, but also there is problem with this because if there is single qoutes in name than everything meesses up because of that qoutes

This is the problem with generating HTML by mashing strings together instead of using DOM.

You have to very carefully escape everything.

Use DOM instead.

Since you are using jQuery already. Something along these lines should get you started:

var url = "users/" + encodeURIComponent(dataItem.id) + "/editUser.html?id=" + encodeURIComponent("${id}") + "&name=" + encodeURIComponent("${name}");
var $link = jQuery("<a/>")
            .attr("href", url)
            .attr("title", "${editTr}")
            .attr("id", "edit") // Are you sure this is a UNIQUE id? Should this be a class instead?
            .on("click", edit);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335