[Home]WikiPatches/OffsiteNewWindow

UseModWiki | WikiPatches | RecentChanges | Preferences

This patch enables all links within wiki that start with http:// to launch into a new window. It also forces a new page to launch when "Preview" and "Save" is used on the edit page.

I did a little editing of the instructions here so that they make a bit more sense, IMHO. The original text (see below) had some interesting mix of bullet text that I think may have contributed to some people trying to make this work and failing. Because it worked for me, I figured that there was simply a problem with the explaination of what it does and how to patch it. It doesn't pop a new window for all "off site" or assess whether a link is within a domain, or even within the wiki... Instead it seems to detect the link beginning with http:// and then pops a new window if that is true. Unexpectedly, after doing the patch, I found that while editing pages (regardless of whether the page had an "http://" on it, the Preview and Save buttons launch a new window. This is not desirable! (Did I miss something? I really don't think so... this seems to be the way the patch works.) If this works well, this patch might be especially helpful when trying to put wikis into frames or iframes. Perhaps there is a way to use it to force all links to target=_top, which in turn might solve te problem of off site pages loading within a frame? Hmmmm -- MatthewSimpson

I've just applied this patch and, yes, it's great that http:// links open in a new window, it helps to keep things clear. But the fact that the saved version of an edit (upon pressing Save) opens in a new window is more confusing, particularly as the edit window remains open. Is this fixable?? StephenHay

Can someone fix this ^^ i would only like to have external links / intermap to open in a new window. I cant find where to add the target="_blank" ...

Ok i changed this

  if ($useImage && &ImageAllowed?($url)) {
    return "<img src=\"$url\">";
  }
  return "<a href=\"$url\">$name</a>";
}

to

  if ($useImage && &ImageAllowed?($url)) {
    return "<img src=\"$url\">";
  }
  return "<a href=\"$url\" target=\"_blank\">$name</a>";
}

Dont know if this is right but it seems to work for me...

Step 1 - add this to the list of global use vars

$PopWindows

Step 2 - add the following below the list of # Major Options:

# Patch for WikiPatches/OffsiteNewWindow
$PopWindows = 1; # 1 = offsite links target _blank, 0 = all links target _self
Step 3 - within sub ScriptLink add
  my $target = "";
  if ($PopWindows) {
    $target = "target=\"_self\"";
  } else {
    $target = "";
  }
Step 3a - in the same sub routine (ScriptLink) change the return line so that it reads
  return "<a $target href=\"$ScriptName?$action\">$text</a>"; 

Step 4 - within sub routine GetHtmlHeader add

  if ($PopWindows) {
    $html .= qq(<BASE target="_blank">\n);
  }


THIS IS WHAT YOU WANT RIGHT HERE

The above implementation of this seems really backwards. Why would you want to set opening in a new page as the default (via the BASE tag) and then override this on all intrawiki links? I just implemented this myself in what I feel is a much more sensible way. I added a new configuration option:

$OffsiteNewWindow = 0;	# 1 = Offsite links open in new window

Then I changed the last line of UrlLinkOrImage? to:

my $target = '';
$target = 'target="_blank"' if ($OffsiteNewWindow);
return "<a href=\"$url\" $target>$name</a>";

and the last line of StoreBracketUrl? to:

my $target = '';
$target = 'target="_blank"' if ($OffsiteNewWindow);
return &StoreRaw("<a href=\"$url\" $target>$text</a>");

These changes make it so that all unencapsulated URLs, bracket URLs starting with http, InterWiki links, and links to uploaded files open in new windows, leaving other links unchanged.

-- Mapache

I modified your code a bit to allow for pages from my [site] to load in the same window. I added a configuration option to hold a string to be compared:

$OffsiteNewWindow = 0;      # 1 = display in new window 0 = display in same window
$OnsiteBase = '';            # If entered will not be displayed in a new window

and modified the if statements above:

$target = 'target="_blank"' if ($OffsiteNewWindow && $url !~ /$OnsiteBase/i);
return "<a href=\"$url\" $target>$name</a>";

-- BobBreedlove

A couple of addon notes to these patches . . .

  1. $OffsiteNewWindow? has to be declared on line 34 of wiki.pl. stick it inside the parenthesis of qw()
  2. To save yourself some grief, do not add the new config parameters to the very end of the config file. I had to put mine elsewhere in the file or else i got an Error 500.
After figuring these 2 items out, it ran like a champ.

-- DarrinLowery?


Original Text

I've added an option to cause offsite site links to spawn a new window

  my $target = "";
  if ($PopWindows) {
    $target = "target=\"_self\"";
  } else {
    $target = "";
  }
  if ($PopWindows) {
    $html .= qq(<BASE target="_blank">\n);
  }
what would be nice is for a user to select their preference --EricScheid Unfortunately if you make that a user preference, it will break the page caching feature.

Does anyone has something that works? Unfortunately, this does not works and all links starts opening on new windows after the changes. I saw no reference to check if the referer is not from within the domain where the wiki is running on and such. Bottom line, it does not works.


Using the <BASE> tag is just plain incorrect, period. It is a head-level tag, for one. It changes the default for all links on the page globally, and the fact that it happens to work on some browsers and not do so for links previous doesn't change the intended behavior -- browsers may eventually fix their behavior. You need to set the target= attribute per-link. I don't have the script handy to tell you where at the moment, but ScriptLink is most likely at least one place it needs to be done. --ChuckAdams


UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited November 13, 2007 11:25 pm by MarkusLude (diff)
Search: