In reponse to this question I wrote this function (well, I made it a bit more elaborate then in that answer):
function redirectto(url) {
window.location.href = url; // first try it the easy way
// we're going to do it the hard way - create a temporary form and submit it
var tmpform = document.createElement("form");
tmpform.method = "GET";
// add data, use hidden fields for querystrings
if (url.indexOf("?") == -1) {
tmpform.action = url;
} else {
var urlparts = url.split("?", 2);
tmpform.action = urlparts[0];
var queryparts = urlparts[1].replace(/\+/g, " ").split(/[&;]/g);
for (var i = 0; i < queryparts.length; i++) {
var pair = queryparts[i].split("=");
var key = pair[0];
var value = pair.length > 1 ? pair[1] : "";
var field;
try { // sigh IE, can't you do ANYTHING right?
field = document.createElement("<input type=\"hidden\" name=\"" + key + "\" value=\"" + value + "\">");
} catch(err) {
field = document.createElement("input");
field.type = "hidden";
field.name = key;
field.value = value;
}
tmpform.appendChild(field);
}
}
// add to page and submit
document.body.appendChild(tmpform);
tmpform.submit();
}
The answer I wrote got 3 downvotes, so my question is: is this the correct way to do this or is simply window.location.href = url enough?
window.location.href = urlis a valid way of redirecting the browser. Typically I discourage browser redirection unless absolutely necessary. There tend to be few good reasons to redirect via JS.