0

I have a button at the bottom of a form that when is pressed I would like to open up a new URL in the same window. Right now with the code I have it opens up a new window /tab. If I put ,"_self" after the url, the button breaks completely. Here is my code

 $(".study-btn").click(function(){
    if(questionCounter == 3)
    {
        window.open("http://www.google.com");
        // I tried this but breaks completely
        window.open("http://www.google.com", "_self");
    }
    else
    {
        window.open("http://http://www.amazon.com");
    }

});

The question counter is just to me to figure out which of the form questions have been answered YES or NO.

icekomo
  • 9,328
  • 7
  • 31
  • 59
  • 1
    possible duplicate of [Open a URL in a new tab using JavaScript](http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript) –  Mar 18 '15 at 20:02

2 Answers2

0

Using this should work to open a website in a new tab in the same window:

window.open('www.google.com','_target');

If you just want to change the current page, use:

location = 'http://www.google.com';
ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38
0

This should work if you want to stay in the same window. window.open AFAIK usually opens a new "pop-up" window, which you don't need if you want to stay in the current window.

$(".study-btn").click(function(){
    if(questionCounter == 3)
    {
        location.href="http://www.google.com";
    }
    else
    {
        location.href="http://http://www.amazon.com";
    }
});
Phil
  • 4,029
  • 9
  • 62
  • 107
  • this seems to just reload the page with the answers from the form in the URL – icekomo Mar 18 '15 at 20:06
  • @icekomo can you set up a simple JsFiddle to show what you have now and what you wish to accomplish? Looks like you may need to set your FORM method to POST if you're seeing values in the URL. – Phil Mar 18 '15 at 20:07