The CreatorCon Call for Content is officially open! Get started here.

Knowledge articles have problems with opening implicit links in our instance

amarks
Tera Expert

Folks, we are having an issue with our knowledge articles that contain external links. They are failing when the link is accessed by our technicians but open without issue from the portal side. It was suggested we should ensure all links have the target = '_blank' in the URL (which is defaulting to a new, external window), which we are doing and which works.  The default behavior in our instance is that this tag is added automatically when creating a new URL using the link button in the editor or when the URL is typed into the field in the article. However, if the URL is pasted into the article, it does not insert the target tag, and the link will not resolve with a message "connection refused." 

 

My question is, is an SSO issue? Does anyone else have this issue and; if so, what are you doing about it?

 

Thanks for your feedback,

Adam

1 ACCEPTED SOLUTION

@amarks 

You can use the following code to add target="_blank" to the content but updating the same back to article record will not work, because the HTML tags will be stripped before updating the record.

 

var gr = new GlideRecord("kb_knowledge");
gr.addEncodedQuery('textLIKE</a>^textNOT LIKEtarget="_blank"');
gr.setLimit(2);
gr.query();

while(gr.next()){
    var htmlContent = gr.getValue("text");
    var updatedHtmlContent = addTargetBlankToAnchorTags(htmlContent);
    gs.print(updatedHtmlContent)
}

function addTargetBlankToAnchorTags(htmlContent) {
  var regExp = /<a href="([^"]+?)"([^>]*)>/g;
  htmlContent = htmlContent.replace(regExp, function(match, href, attributes) {
    if (!attributes.includes('target="_blank"')) {
      attributes += ' target="_blank"';
    }
    return '<a href="' + href + '"' + attributes + '></a>';
  });
  return htmlContent;
}

 

Please mark my answer helpful and accept as solution if it helped 

Thanks,
Anvesh

View solution in original post

6 REPLIES 6

AnveshKumar M
Tera Sage
Tera Sage

Hi @amarks 

 

We had similar issue but not with Knowledge article, it is with another table records. The users need to right click and open in new tab otherwise the link will try to open in the same Frame and throws an error.

 

I can say that this issue is not with SSO. And I'm not sure about the editor why it is not adding the attribute when it is pasted.

 

We have updated all the records using a fix script to add target="_blank" attribute.

 

This is because of X-Frame-Options to sameorigin error in the Console. Which means, the URL you are using has a restriction that, the content can not be embedded in another domain (URL) page (ServiceNow will try to display the content in its own window if you are on the Home window of SN, but you may not get this issue if you open your KB article in a new tab and try clicking on the external link).

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0778480

 

 

Please mark my answer helpful and accept as solution if it helped 👍

Thanks,
Anvesh

Thanks AnveshKumar,

 

I appreciate the feedback. I'm guessing if the expected behavior is for ServiceNow to open in a new tab then we should report this as a bug.

 

Can you share any details about the fix script? I'm new to ServiceNow and don't know the backend, but I was able to write a python script to identify all the articles that do not have the target="_blank" attribute.

 

Again, thanks for your assistance,

Adam

@amarks You can use the following script in Background Script to get the article which doesn't have target="_blank".

 

var gr = new GlideRecord("kb_knowledge");
gr.addEncodedQuery('textLIKE</a>^textNOT LIKEtarget="_blank"');
gr.query();

while(gr.next()){
    gs.print(gr.number);
}

 

Please mark my answer helpful and accept as solution if it helped you 👍✔️

Thanks,
Anvesh

@amarks You can also use a filter like the one shown below in list view.

 

AnveshKumarM_0-1698846639478.png

 

Please mark my answer helpful and accept as solution if it helped you 👍✔️

Thanks,
Anvesh