User talk:Mike Dillon/Scripts/params.js

From Wiktionary, the free dictionary
Latest comment: 16 years ago by Ruakh
Jump to navigation Jump to search

Currently this script does nothing to remove the URI's fragment if it has one. I'd recommend doing that right before checking the memo cache:

// If no URL is passed in, use the current page's URL
if (!url) {
    url = location.href;
}

// Remove fragment, if any
url = url.split("#", 2)[0];

// If the parameters for this URL have already been parsed, return them
if (urlParamMaps[url]) return urlParamMaps[url];

(That's not how I'd think to remove the fragment, but it's the most consistent with your scheme for extracting the query.)

Incidentally, the whole assign-to-a-closure technique for creating a function with a static local variable? Brilliant! I'm totally going to start using that myself.

RuakhTALK 18:12, 22 February 2008 (UTC)Reply

I think another way to do that is to use window.location.pathname instead. I'd have to verify the cross-browser compatibility though. Mike Dillon 19:01, 22 February 2008 (UTC)Reply
It might also make sense to make getParameterValue take the last value instead of the zero-th value. I think I modelled this after Java's HttpServletRequest methods originally. Mike Dillon 19:03, 22 February 2008 (UTC)Reply
From Googling, I get the impression that window.location.pathname is just the hierarchical part (so, excluding the query); however, window.location.search is apparently the query plus leading '?'. I've tested this in Firefox 2 and IE 7, and it works in both; if it works more generally, then I think that's the thing to use. —RuakhTALK 19:50, 22 February 2008 (UTC)Reply
Yeah. My comment was kind of dumb. Mike Dillon 19:54, 22 February 2008 (UTC)Reply
http://developer.mozilla.org/en/docs/DOM:window.location#Properties describes window.location.search as non-standard DOM level 0, which I think means it has good browser support. I'm going to change MediaWiki:Preloadtitle.js to use it. —RuakhTALK 19:57, 22 February 2008 (UTC)Reply