Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Commonmark migration
Source Link

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many security measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise, you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can help tell whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many security measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise, you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can help tell whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many security measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise, you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can help tell whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}
fixed wordyness and small issues
Source Link

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many securesecurity measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise, you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can be helpful in tellinghelp tell whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many secure measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can be helpful in telling whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many security measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise, you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can help tell whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}
added 43 characters in body
Source Link

Standard "vanilla" JavaScript way to redirect a page:

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many secure measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can be helpful in telling whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}

Standard "vanilla" JavaScript way to redirect a page:

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:


The following section is for those using HTTP_REFERER as one of many secure measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can be helpful in telling whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}

Standard "vanilla" JavaScript way to redirect a page

window.location.href = 'newPage.html';

Or more simply: (since window is Global)

location.href = 'newPage.html';

If you are here because you are losing HTTP_REFERER when redirecting, keep reading:

(Otherwise ignore this last part)


The following section is for those using HTTP_REFERER as one of many secure measures (although it isn't a great protective measure). If you're using Internet Explorer 8 or lower, these variables get lost when using any form of JavaScript page redirection (location.href, etc.).

Below we are going to implement an alternative for IE8 & lower so that we don't lose HTTP_REFERER. Otherwise you can almost always simply use window.location.href.

Testing against HTTP_REFERER (URL pasting, session, etc.) can be helpful in telling whether a request is legitimate. (Note: there are also ways to work-around / spoof these referrers, as noted by droop's link in the comments)


Simple cross-browser testing solution (fallback to window.location.href for Internet Explorer 9+ and all other browsers)

Usage: redirect('anotherpage.aspx');

function redirect (url) {
 var ua = navigator.userAgent.toLowerCase(),
 isIE = ua.indexOf('msie') !== -1,
 version = parseInt(ua.substr(4, 2), 10);
 // Internet Explorer 8 and lower
 if (isIE && version < 9) {
 var link = document.createElement('a');
 link.href = url;
 document.body.appendChild(link);
 link.click();
 }
 // All other browsers can use the standard window.location.href (they don't lose HTTP_REFERER like Internet Explorer 8 & lower does)
 else { 
 window.location.href = url; 
 }
}
added 96 characters in body
Source Link
Loading
Not using block quotes for emphasis.
Source Link
duplode
  • 34.5k
  • 7
  • 88
  • 161
Loading
Copy edited.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134
Loading
added more details
Source Link
Loading
added explanation
Source Link
Loading
added explanation
Source Link
Loading
added explanation
Source Link
Loading
added 27 characters in body
Source Link
Loading
.replace redirects the page without adding a new record to "history". location.href is what we need here.
Source Link
Loading
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/Uniform_Resource_Locator>).
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134
Loading
added 3 characters in body
Source Link
Loading
changed variable name to make more sense
Source Link
Loading
added 7 characters in body
Source Link
Loading
Loading
added 1 characters in body
Source Link
Loading
fixed verOffset
Source Link
Loading
added 8 characters in body
Source Link
Loading
fixed for IE8 and lower support
Source Link
Loading
added note about referer spoofing
Source Link
Loading
added 183 characters in body
Source Link
Loading
added 7 characters in body
Source Link
Loading
Source Link
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /