JavaScript loads the page when a button is clicked

I am trying to perform a very simple task here, I would like to be able to click a button on a page and transfer it to another page. I tried window.location.href and a bunch of other things, and it does nothing. I tried different platforms and different browsers, all with the same result.

I know that it can call a function, but I just cannot load a new page. Also, this is all on my local file system, and both pages live on the same level (but I also tried loading an external page such as www.apple.com).

Any thoughts?

Thanks Patrick

+43
javascript button load
Sep 10 '10 at 7:08
source share
4 answers

Simple code to redirect a page

<!-- html button designing and calling the event in javascript --> <input id="btntest" type="button" value="Check" onclick=window.location.href = 'http://www.google.com' " /> 
+72
Sep 10 '10 at 7:17
source share

Do not use form elements where <a> elements will be enough.

 <style> /* or put this in your stylesheet */ .button { display: inline-block; padding: 3px 5px; border: 1px solid #000; background: #eee; } </style> <!-- instead of abusing a button or input element --> <a href="url" class="button">text</a> 
+18
Sep 10 '10 at 7:16
source share

Just window.location = "http://wherever.you.wanna.go.com/" , or for local links window.location = "my_relative_link.html" .

You can try typing it into your address bar, for example, javascript: window.location = "http://www.google.com/" .

Also note that part of the URL protocol ( http:// ) is not optional for absolute links; omitting it, it will make a relative javascript link.

+15
Sep 10 '10 at 7:15
source share

Here are the answers to open the page in the same browser window / tab.

However, I wanted the page to open in a new window / tab when they click the button. (tab / window depends on the user's browser settings)

So here is how it works: open the page in a new tab / window :

 <button type="button" onclick="window.open('http://www.example.com/', '_blank');">View Example Page</button> 

This is not a required button; you can use it anywhere. Pay attention to _blank , which is used to open in a new tab / window.

+3
Mar 27 '17 at 3:03 on
source share



All Articles