Code to open in new tab/window not working

Hiya @mnikitina

We have some code that changes the html of a html container, but we want it to open in a new tab or window:

    //Check if email already exists and update html content to inform user
    pnp.sp.web.lists.getById('5b284401-fc73-4ae5-8b97-5e7178fb89fd').items
        .filter("Email eq '" + fd.field('Email').value + "'").select('ID,Display_x0020_Name').get().then(function(contact){
            console.log(contact);            
            if(contact.length > 0) {
                //Update HTML via Javascript
                $('.FoundUser').html("The contact <a href='" + _spPageContextInfo.webAbsoluteUrl + "/SitePages/PlumsailForms/Contacts/Item/EditForm.aspx?item=" + contact[0].Id +
                "' target='_blank'>" + contact[0].Display_x0020_Name + "</a> has this email address, please update it!")
            }
        })

The link is created to take you to the item, but it opens in the same window, we need it to open in a new window or tab or even a dialog, please can you help?

Thanking you much!

Dear @Jamal_Smith-Graham,
Should be an easy fix, try this:

$('.FoundUser').html("The contact <a href='" + _spPageContextInfo.webAbsoluteUrl + "/SitePages/PlumsailForms/Contacts/Item/EditForm.aspx?item=" + contact[0].Id +
                "' target=\'_blank\'>" + contact[0].Display_x0020_Name + "</a> has this email address, please update it!")

hiya @Nikita_Kurguzov unfortunately, it doesn't work! :frowning: it opens in the same tab, can you think of something else?

Dear @Jamal_Smith-Graham,
Have you tried different browsers/different PCs? This behaviour is ultimately controlled by the browser's settings, and if it's disabled for the browser, it might not be possible to implement.

If target='_blank' works for other generic links - it should also work here, for example, you can try:

<a href="https://google.com" target="_blank">Google</a>

I've also tested, and the following code works for me just fine:

fd.spRendered(function(){
 var listId = fd.spFormCtx.ListAttributes.Id;
 $('#add-link').html('The contact <a href="' + _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/listform.aspx?PageType=6&ListId=' + listId +'&ID=2" target="_blank">Test</a> has this email address, please update it!');
});

I recommend this type of link (with ListID, instead of directly to form page) as it will also check all routing conditions. You can also give it a try. If the List is not the same you're currently working with on the form, you'll have to get ListID in List Settings URL of the list you want to open:
image

So it will look like this:

fd.spRendered(function(){
 var listId = 'f16cd8fc-0f04-4b29-b5ba-1b4bd247c285';
 $('#add-link').html('The contact <a href="' + _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/listform.aspx?PageType=6&ListId=' + listId +'&ID=2" target="_blank">Test</a> has this email address, please update it!');
});

Finally, if the browser doesn't support opening forms in another tab, you can use our Dialog instead - https://plumsail.com/docs/forms-sp/javascript/dialog.html

1 Like

Hiya @Nikita_Kurguzov
Your initial answer actually worked, but the problem seems to be some sort of conflict with your help desk plugin (I am trying to do this with the requester new form). When I do your solution on any other form that didn't come with the plugin it works.

Can you give me an example of how to do the dialog in this case within a $('#add-link).html() as I think it should work in your plugin? I tried to figure it out myself and just got very confused! lol