1

I know this has been asked before both on here and elsewhere, but everything I've tried and read is still failing for me.

I have (so far) tried the following inside a method I am successfully calling:

    window.location.href = "http://www.google.co.uk";
    window.location.replace = "http://www.google.co.uk";
    window.location = "http://www.google.co.uk";
    window.navigate = "http://www.google.co.uk";
    location.replace("http://www.google.co.uk");
    window.location.assign("http://www.google.co.uk");
    self.location = "http://www.google.co.uk";
    top.location = "http://www.google.co.uk";
    location.assign("http://www.google.co.uk");

This is inside a method I am calling from a button:

<button class="btn btn-default" id="SaveAndExit" onclick="saveAndExit();">GO TO GOOGLE</button>

This should be straightforward for something I've broken down so simple, but my screen just refreshes without navigating to the URL I specify.

What can't I see please?!

4
  • 2
    Try with setting type="button" of button, apart from that it should work Commented Dec 21, 2016 at 12:37
  • Is this button in a form that's being submitted? When you debug this, what is actually happening? Is the redirect line of code being reached? What network requests are being made? Commented Dec 21, 2016 at 12:39
  • for better testing please use console.log('Go') before ` window.location.href = "google.co.uk";` . I think your code not reaching this line. Commented Dec 21, 2016 at 12:46
  • Thanks Satpal, that was what I needed. Commented Dec 21, 2016 at 13:55

2 Answers 2

3

The default type of a HTML button is submit, in this case onclick does not run, use

<button type="button" class="btn btn-default" id="saveAndExit" onclick="saveAndExit()">
Sign up to request clarification or add additional context in comments.

Comments

2

You should have an <input type="button"> instead of a button component.

So your button should look like:

<input type="button" class="btn btn-default" id="SaveAndExit" onclick="saveAndExit();">GO TO GOOGLE</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.