Advertisement
Guest User

Open Links In New Window

a guest
Oct 9th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. add_action('wp_head', 'external_links_in_new_windows_client');
  2. function external_links_in_new_windows_client()
  3. {
  4.  
  5.   $blogdomain = parse_url(get_option('home'));  
  6.   echo "<script type=\"text/javascript\">//<![CDATA[";
  7.   echo "
  8.     function external_links_in_new_windows_loop() {
  9.         if (!document.links) {
  10.             document.links = document.getElementsByTagName('a');
  11.         }
  12.         var change_link = false;
  13.         var force = '".get_option("external_links_in_new_windows_force")."';
  14.         var ignore = '".get_option("external_links_in_new_windows_ignore")."';
  15.  
  16.         for (var t=0; t<document.links.length; t++) {
  17.             var all_links = document.links[t];
  18.             change_link = false;
  19.            
  20.             if(document.links[t].hasAttribute('onClick') == false) {
  21.                 // forced if the address starts with http (or also https), but does not link to the current domain
  22.                 if(all_links.href.search(/^http/) != -1 && all_links.href.search('".$blogdomain['host']."') == -1) {
  23.                     // alert('Changeda '+all_links.href);
  24.                     change_link = true;
  25.                 }
  26.                    
  27.                 if(force != '' && all_links.href.search(force) != -1) {
  28.                     // forced
  29.                     // alert('force '+all_links.href);
  30.                     change_link = true;
  31.                 }
  32.                
  33.                 if(ignore != '' && all_links.href.search(ignore) != -1) {
  34.                     // alert('ignore '+all_links.href);
  35.                     // ignored
  36.                     change_link = false;
  37.                 }
  38.  
  39.                 if(change_link == true) {
  40.                     // alert('Changed '+all_links.href);
  41.                     document.links[t].setAttribute('onClick', 'javascript:window.open(\\''+all_links.href+'\\'); return false;');
  42.                     document.links[t].removeAttribute('target');
  43.                 }
  44.             }
  45.         }
  46.     }
  47.    
  48.     // Load
  49.     function external_links_in_new_windows_load(func)
  50.     {  
  51.         var oldonload = window.onload;
  52.         if (typeof window.onload != 'function'){
  53.             window.onload = func;
  54.         } else {
  55.             window.onload = function(){
  56.                 oldonload();
  57.                 func();
  58.             }
  59.         }
  60.     }
  61.  
  62.     external_links_in_new_windows_load(external_links_in_new_windows_loop);
  63.     ";
  64.  
  65.   echo "//]]></script>\n\n";
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement