Redirect not happening After button click but same code works from Dev Tools Console

I have the following JS function:

        function cancelClick() {
            location.replace('Employers_Welcome.aspx');
            return false;
        };

And then I have the following htmllocated within an ASP.NET WebForms page:
            <div class="divTableCell-2">
                <div class="field-container">
                    <button id="CancelBtn" class="ViewLogButton buttonStyle" onclick="cancelClick()">Cancel</button>
                </div>
            </div>

When I click on the button the cancelClick() function is reached ( I can see this in the Debugger). However the redirection does not occur and instead the page just reloads.

However, if I instead call cancelCick() directly from the IE11 Dev Tools Console on that same page the redirect works.

What am I missing? For what it is worth I have tried different variations of the actual JS redirect code (document.href, location.href etc) and I got the same outcome with all of them.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This button may be in a form. Given that the default type of a button is sumbit the form is being submitted and the page reloads. Adding a type=button will remove the default submit behavior from the button.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x