Avatar

It's for detecting if you're loading the page with a #hash on the url; so the AJAX works correctly with bookmarks

1
2
3
4
5
6
7
8
9
10
11
var hash = new String(document.location).indexOf("#");
if(hash > 0)
{
	page = new String(document.location).substring(hash + 1);
	if(page.length > 1)
		loadURL(page);
	else
		loadURL('news');
}
else
	loadURL('news');

Refactorings

No refactoring yet !

035687df00d162cec025302373ebc076

chovy.myopenid.com

October 6, 2008, October 06, 2008 02:05, permalink

1 rating. Login to rate!

Get position of "#" in the location.
Check if we have a "#" at all (-1 is "none")
Get substring from starting position to end of location length.

Uses a two ternary statements to set has_hash boolean, and defining the page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*

var biz;
if ( some_test ) { biz = 'foo' } else { biz = 'bar' }

can be written as a one-line ternary:

var biz = some_test ? 'foo' : 'bar';

*/

var pos_hash = document.location.indexOf("#");
var has_hash = pos_hash > 0 ? true : false;
var page = has_hash ? document.location.substring(pos_hash + 1, document.location.length) : 'news';

loadURL(page);
5071c5b861341c0dcfcf6ac86327701f

Tien Dung

October 6, 2008, October 06, 2008 04:21, permalink

2 ratings. Login to rate!
1
2
3
4
5
6
7
8
// Don't know why you need hash > 0 and page.length > 1
// In case you don't need those conditions
var page = document.location.toString().split("#")[1];
LoadURL(page || 'news')

// In case you need them
var parts = document.location.toString().split("#");
LoadURL((parts[0].length > 0 && parts[1].length > 1) ? parts[1] : 'news')
3c2a50916630af4097799a1fd2b1ebce

Gabriele Lana

October 6, 2008, October 06, 2008 06:13, permalink

No rating. Login to rate!

Not tested but should work. The function selectedAnchorOr is for better express the intent of the coder (plus, I know you have this snipped of code repeated somewhere else in your project, a function can help to keep your code more DRY and readable)

1
2
3
4
5
6
7
8
9
var selectedAnchorOr = function(url, default) {
  var urlWithAnchor = url.match(/^.*(#.+)$/);
  if (urlWithAnchor !== null) {
    return urlWithAnchor[1];
  }
  return default;
}

loadUrl(selectedAnchorOr(document.location, "news"));
035687df00d162cec025302373ebc076

chovy.myopenid.com

October 6, 2008, October 06, 2008 19:46, permalink

No rating. Login to rate!

Some would say you should only have one return point from a function. And there's no need to pass the location object each invocation.

1
2
3
4
5
6
7
var getAnchor = function (default) {
  default ||= 'news';
  return document.location.toString().split( '#' )[1] || default;
};

loadURL(getAnchor()); // default is used if no anchor
loadURL(getAnchor("help")); // default is set to 'help' and used if no anchor
5071c5b861341c0dcfcf6ac86327701f

Tien Dung

October 6, 2008, October 06, 2008 22:23, permalink

1 rating. Login to rate!

You can't do ||= in JavaScript

1
2
3
4
function getAnchor(default) {
  default = default || 'news';
  return document.location.toString().split( '#' )[1] || default;
};
A8d3f35baafdaea851914b17dae9e1fc

Adam

October 8, 2008, October 08, 2008 03:59, permalink

2 ratings. Login to rate!

No need to split up the location string.

1
loadURL(location.hash || "#news")

Your refactoring





Format Copy from initial code

or Cancel