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

no edit summary
No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 33:
category = document.referrer.match(/\/Category:([^\/]+)$/)[1];
if(category == null) return;
if(!document.body.innerHTML.indexOf("\"/wiki/" + category + "\"")) return; //EDIT: just added this line to try to prevent it from redirecting pages that aren't in the category (and thus don't have a solution or anchor) to the categories section. Now it isn't tested, but it should still work.
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 categorythink templateit's asfixed wellnow).
::::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). It will also have the unwanted side effect of changing ALL the links on the page.
::::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). Also, the second one modifies ''all'' links on a page. ˙ʇuǝɯǝʌoɹdɯı ǝɯos spǝǝu ʇı os [[User:BR|BR]] 06:34, 17 November 2010 (UTC)
Anonymous user