4

I am trying to navigate to another page using jquery

Markup :

<input type="image" name="ImageButton1" id="btnCancel" src="../images/btn_cancel.gif" />

JS

 $('input[type="image"][id^="btnCancel"]').live('click', function () {
        window.location.replace('default.aspx');
 });

Page just refreshes, it does not navigate to desired page. when i change type=button it works.

How to achieve the same by keeping type as image only ?

Shaggy
  • 5,422
  • 28
  • 98
  • 163

3 Answers3

4
$("#btnCancel").on('click', function () {
        window.location.href = 'default.aspx';
});
Nikhil Talreja
  • 2,754
  • 1
  • 14
  • 20
  • Why to use `href` over `replace` ? – Shaggy Jun 26 '14 at 09:19
  • Difference is only the way they manipulate browser history: http://stackoverflow.com/questions/7703689/difference-between-window-location-href-window-location-replace-and-window-loca – Nikhil Talreja Jun 26 '14 at 09:27
3

Use preventdefault:

$('input#btnCancel').on('click', function (e) {
        e.preventDefault();
        window.location.replace('default.aspx');
 });
Arvind Bhardwaj
  • 5,231
  • 5
  • 35
  • 49
1

Us on instead of live...and prevent default to stop the default behaviour.

  $(document).on('click','input[type="image"][id^="btnCancel"]',function(e){
       e.preventDefault();
       window.location.replace('default.aspx');
  });

live is deprecated

Kylie
  • 11,421
  • 11
  • 47
  • 78