workaround: link to bookmark anchor on same page in sharepoint 2007 wiki

Published: November 30, 2008

The wiki functionality provided by Microsoft in Sharepoint 2007 (MOSS 2007) is somewhat limited. One limitation is that you cannot use anchor tags to link to a bookmark anchor on the same page. So setting up a TOC at the top of a wiki page to let users jump down to specific page sections will not work. Below is the workaround I came up with in order to get links to bookmarks in the same page working.

Part 1 - Custom JavaScript include

This JavaScript file looks for instances of wiki anchor tags where the href attribute contains either of two values (EditForm or CreateWebPage). Why look for those two values? Because that is what is automatically inserted by sharepoint when you save a wiki page with an anchor tag pointing to a bookmark anchor. If those values are found they are stripped from the href attribute so that the link to the bookmark anchor works.
  1. Save the wikilinkfix.js file to \\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\
  2. Open the wiki standard template located at \\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\DocumentTemplates\wkpstd.aspx
  3. Insert the wikilinkfix.js as a javascript include file on the wkpstd.aspx page: <script type="text/javascript" src="/_layouts/1033/wikilinkfix.js"></script>

Note: Including this script on the standard wiki template will only apply the fix for newly created wiki pages. In order to make the fix on existing wiki pages you will need to either manually add the script include using sharepoint designer or rebuild your wiki pages. Another option is to just add the script include to your master page. Then the fix would be applied to both new and existing wiki pages.

Part 2 - Edit the core.js files

The following edits are necessary so that end users can create links to bookmark anchors using the hyperlink button in the wiki rich text editor.
  1. Open the Core.js file located at \\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\INC\Core.js
  2. Add "#", to the Hyperlink.arrAllowedProtocols comma delimited list.
Hyperlink.arrAllowedProtocols = [

  1. Open the CORE.JS file located at \\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033\CORE.JS
  2. Add href.match(new RegExp("^#", "i")) || to the IsSafeHref function.
function IsSafeHref(
	return (href.match(new RegExp("^http://", "i")) ||
			href.match(new RegExp("^https://", "i")) ||
			href.match(new RegExp("^#", "i")) ||
			href.match(new RegExp("^ftp://", "i")) ||
			href.match(new RegExp("^file://", "i")) ||
			href.match(new RegExp("^mailto:", "i")) ||
			href.match(new RegExp("^news:", "i")) ||
			href.match(new RegExp("^/", "i")) ||
			href.match(new RegExp("^\\\\\\\\", "i")));

Part 3 - Notes

I have tested this on Windows XP with IE 7 and Firefox 2. If anyone notices any issues let me know. The wikilinkfix.js script looks for (EditForm or CreateWebPage) because those are the only two culprits I have come across. If anchor tags are modified by sharepoint with different strings then they can be added to the script.

Right now you cannot create a bookmark anchor tag such as <a name="bottom"></a> using the wiki rich text editor. A bookmark anchor can be added using the source editor. An improvement would be to extend the insert hyperlink button so that an anchor tag could be inserted this way.