We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    Is there a plug-in available to change target="_blank" to rel="external" to fix validation available? I think that this would be easy but my brain is shot today (it has been one of those Mondays). Could someone give me a push in the right direction and see if I can’t ’jump start’ my brain to work this out rolleyes
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don't know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 25969
      • 118 Posts
      // Get the whole page, right before it's rendered to the browser
      $source = &$modx->documentOutput;
      
      // Syntax to find and replace
      $strFind = 'target="_blank"';
      $strRepl = 'rel="external"';
      
      // Do the thing that finds and replaces stuff
      $source = str_replace($strFind, $strRepl, $source);
      


      Call the plugin what ever you like and check OnWebPagePrerender under System Events for it.
        • 25969
        • 118 Posts
        I was gonna say, you don’t need rel="external" if you only intend to open actual external links in a new window. If using jQuery for example, you can just

        jQuery.noConflict();
        jQuery(document).ready(function($){
        	// Open external links in a new window
        	$('a[href^=http]').each(function(){
        		if(this.href.indexOf(location.hostname) == -1) {
        			$(this).attr('target', '_blank');
        		}
        	});
        });

        (I usually do my jQuery stuff with noConflict so yeah)
          • 7231
          • 4,205 Posts
          Sal, thanks a lot for both bits of code. I see the light wink

          The jQuery is a good idea, I would need to edit it to work with .doc and .pdf files from the current hostname as well as external links.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don't know what it is.
            Do you, Mr. Jones? - [bob dylan]