admin管理员组

文章数量:1386852

Trying to do a redirect with javascript using the following code (works fine in ALL browsers except IE8 and IE7):

if (confirm('Are you sure you wish to clear your basket?')) {
    window.location.href = '/basket/clear';

    if (navigator.userAgent.search('MSIE') > -1) alert('Your basket has been cleared.');
}

I have added in the second alert stating that the basket has been cleared so that the code works in IE. If I remove the alert then it doesn't work.

Any ideas or solutions would be greatly appreciated.

Trying to do a redirect with javascript using the following code (works fine in ALL browsers except IE8 and IE7):

if (confirm('Are you sure you wish to clear your basket?')) {
    window.location.href = '/basket/clear';

    if (navigator.userAgent.search('MSIE') > -1) alert('Your basket has been cleared.');
}

I have added in the second alert stating that the basket has been cleared so that the code works in IE. If I remove the alert then it doesn't work.

Any ideas or solutions would be greatly appreciated.

Share Improve this question asked Jan 2, 2011 at 14:05 ABailissABailiss 1731 gold badge2 silver badges9 bronze badges 5
  • 1 If it's ing from href of anchor tag you must also have return false otherwise the link will be "processed". – user447356 Commented Jan 2, 2011 at 14:33
  • @Shadow Wizard This should be an answer. I, for one, think you are correct. – Hemlock Commented Jan 2, 2011 at 14:58
  • @Hemlock not sure that's the case here.. @ABailiss can you post the code invoking that JavaScript? – user447356 Commented Jan 2, 2011 at 15:15
  • It was a button that called the code, not an anchor tag. Simply adding a return false; after the redirect seems to have fixed things in IE even though it is pointless returning false for a button. – ABailiss Commented Jan 10, 2011 at 18:15
  • what button exactly? If just plain <button onclick="..."> it defaults to submit button that when clicked will submit its form. Anyway, when menting on someone else ment use @ to notify that person (one at a time) otherwise he/she won't see your ment. – user447356 Commented Jan 10, 2011 at 22:01
Add a ment  | 

1 Answer 1

Reset to default 3

try using just window.location = '/basket/clear';

also, when you change the window location you cannot expect code beyond that point to execute, such as an alert

本文标签: Javascript Redirect windowlocationhrefIEStack Overflow