create a redirect to another page upon page load in javascript

To create a redirect to another page upon page load in JavaScript, one way is to use the onload event to trigger the redirection. Here's an example code snippet:

index.tsx
window.onload = function() {
  window.location = "https://example.com/newpage";
};
83 chars
4 lines

Explanation: The onload event is fired when the page finishes loading. In the function, we simply set the value of window.location to the URL of the new page we want to redirect to. This effectively sends the user to the new page.

Note: It is recommended to use a full URL including the protocol (https:// or http://) to avoid ambiguity and potential errors.

gistlibby LogSnag