Trip
Trip

Reputation: 27114

Would anyone refactor a URL change better than the way I do?

Not sure if this is inapropriate albeit it working fine.

In order to move to a new location and retain the root URL, I do examples like this :

window.location = window.location.href.split('/oldbar')[0] + '/foobar/' + $value

window.location = window.location.href.split(/oldbar/)[0] + 'foobar';

window.location = window.location.href.split('/oldbar')[0] + '/foobar/'

Would you do it differently? If so how / why?

Upvotes: 0

Views: 175

Answers (2)

Alnitak
Alnitak

Reputation: 339786

To retain the root URL, just manipulate window.location.pathname, not the whole href property.

Upvotes: 0

karim79
karim79

Reputation: 342635

You don't need to split up the href when you can just access the pathname property of the location object:

// not really sure what you're trying to do, but...
window.location.pathname = window.location.pathname + "/foobar/";

Upvotes: 2

Related Questions