Rosetta Code:Village Pump/Language Page Links: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 27: Line 27:


:::Actually, it took no time at all. I'm done now. I'll post after dinner. [[User:BR|BR]] 04:12, 17 November 2010 (UTC)
:::Actually, it took no time at all. I'm done now. I'll post after dinner. [[User:BR|BR]] 04:12, 17 November 2010 (UTC)

::::To the problem template add: <lang JavaScript>window.onload = function()
{
if(document.referrer == '' || window.location.hash != '') return;
category = document.referrer.match(/\/Category:([^\/]+)$/)[1];
if(category == null) return;
window.location.hash = category;
}</lang> Tested with IE, FireFox, Opera, Chrome, Safari (from multiple categories to multiple problems, including "dud" categories without the code, and from non-categories). Should work with others too, nothing special was done. In IE theres a little bug - go to category, click link, it'll take you to the right section. Press the Back button and it'll take you to the top of the page, hit Back again to get back to the category. In all of them, links from any page that starts with "Category:" to any page with the problem template will attempt to redirect to #category-name (no way around that I can see without modifying the category template as well).
::::Alternatively, add the following code to the category template: <lang JavaScript>window.onload = function()
{
var links = document.links;
var i = links.length;
var category = document.location.href.match(/\/Category\(colon\)([^\/]+)$/)[1];
while (i-- > 0)
links[i].href = links[i].href + "#" + category;
}</lang> I haven't tested this very well - just once in Opera from Category:1 to Problem1 (it's almost time to go to bed).
::::Beware that these might be used to inject a malicious anchor in somehow (haven't thought it through that far, but I'm convinced it could be done somehow). [[User:BR|BR]] 06:34, 17 November 2010 (UTC)