Page Redirect in JavaScript

Page redirects can be used to redirect user to some other webpage or website depending on the choice.

Page Redirect Syntax

As there is not much to explain we directly jump to syntax and get awesome stuff working.

window.location.href = "New Location's URL"

Page Redirect Examples

All we need to do is attaching event Listeners to these buttons and get it working

<button class=”btn center-block btn-primary” onclick=”window.location.href=’https://www.google.com/'”>Google</button>

<button class=”btn center-block btn-primary” onclick=”window.location.href=’https://www.yahoo.com/'”>Yahoo</button>

<button class=”btn center-block btn-primary” onclick=”window.location.href=’https://www.facebook.com/'”>Facebook</button>

<button class=”btn center-block btn-primary” onclick=”window.location.href=’https://www.amazon.com/'”>Amazon</button>

window.location.href = "https://www.google.com/"

Page Printing

Javascript can be used to print the content of the pages into a hardcopy. Below command will open the printer page to print the current contents of the webpage.

window.print()

Simply paste this in your browser’s console and get a hard copy of whatever page you require.

Translate ยป