Add target = "_ blank" to specific links that have not yet set a target

I understand that thousands of similar questions have already been asked, but after 30 minutes of viewing, I could not find the one that relates to my specific situation.

I am looking for a solution to ColdFusion 7/8 or Javascript or JQuery. CF is preferred.

I need a script that will search for a large text string to find all case-insensitive instances ...
<a href="http://[something_external]">
This is NOT a goal. And replace them with ...
<a href="http://[something_external]" target="_blank">

Thus, the criteria for the link to be changed:

  1. The href value begins with "http: //"
  2. Href value does not contain "ourdomain.com"
  3. not specified value

For example, I found the following online, but this is not entirely correct:
REReplaceNoCase(mystring, '(<a href="http://[^"]+")>', '\1 target="_blank">', 'ALL')

Make sense? Doable?

+4
source share
1 answer

JQuery

 $(function() { $.each($('a[href^=http]'), function() { var $this = $(this); $this.attr('href').indexOf('ourdomain.com') >= 0 || typeof($this.attr('target')) != "undefined" || $this.attr('target', '_blank'); }); }); 
  • Scroll through all links starting with http when the document is ready
  • Contains ourdomain.com ?
  • Does the attribute have a target ?
  • Set target attribute
+4
source

Source: https://habr.com/ru/post/1438100/


All Articles