31

I need to stop all the page redirection through javascript.

I have some script which will redirect the page to some other location. How can I stop all the page redirection on my page through jquery or javascript.

Berry M.
  • 469
  • 1
  • 4
  • 17
santose
  • 311
  • 1
  • 3
  • 3

5 Answers5

76

You can stop the redirect by doing the following.

<script language="JavaScript" type="text/javascript">
    //<![CDATA[
        window.onbeforeunload = function(){
            return 'Are you sure you want to leave?';
        };
    //]]>
</script>

Not sure if all browsers support this but that should do the trick.

Nalum
  • 4,143
  • 5
  • 38
  • 55
  • 2
    It's ingenious in it's simplicity. On a sidenote - it's the only method that appears to stop malicious scripts from redirecting using `window.location.replace()` or `window.location.href="virus"` type of things. Helped me with debugging, so thanks a lot. – Krzysztof Jabłoński Nov 10 '18 at 01:23
  • Is it possible to get the URL that it is redirecting to using this method? – NPC Jun 18 '21 at 16:15
2

If I'm understanding you correctly, you need to stop users navigating away from the page?

If so, Marcos Placona's answer is one part of it - although all what you actually need is:

$("a").click(function(e) { e.preventDefault(); });

You also don't want people to hit F5 I'm guessing? Well that's harder. Preventing defaults on key press, cross-browser is horrible. But a couple of codes that work in some browser are below here:

function disableF5() {  // IE
    document.onkeydown = function() {
        if (window.event && window.event.keyCode == 116) { // Capture and remap F5
            window.event.keyCode = 505;
        }
        if (window.event && window.event.keyCode == 505) { // New action for F5
        return false; // Must return false or the browser will refresh anyway
        }
    }
}
function disableF5v2() { // FIREFOX
    $(this).keypress(function(e) {
        if (e.keyCode == 116) {
            e.preventDefault();
    return false;
        }
    });
}

However, the cross-browser issue can partly be solved with -

window.onbeforeunload = function(e) {
    var message = "Your confirmation message goes here.", e = e || window.event;
    // For IE and Firefox 
if (e) {
    e.returnValue = message;
}
// For Safari
    return message;
};

Hope that all helps anyway. The last bit of code is from another question on this site here. Rob

Community
  • 1
  • 1
LiverpoolsNumber9
  • 2,384
  • 3
  • 22
  • 34
2

Are you trying to stop an iframe breaker? Something like:

<script language="JavaScript" type="text/javascript">
<!--
function breakout_of_frame()
{
  if (top.location != location) {
    top.location.href = document.location.href ;
  }
}
-->
</script>

If so, you can not.

Peeter
  • 9,282
  • 5
  • 36
  • 53
  • 1
    HTML5 supports `sandboxed` iframes. "prevent the content to navigate its top-level browsing context". http://www.w3schools.com/tags/att_iframe_sandbox.asp – kev Oct 20 '16 at 01:03
1

If it's only redirects through link clicks, in jQuery you can do this:

$(".someClass a").unbind('click');

if it's an existing page redirect, you'll need to give us some more information about what kind of redirect this is, so we can help further.

Hope this helps you

Harry
  • 87,580
  • 25
  • 202
  • 214
Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
  • 1
    that would assume all anchors are bound by jQuery. To be sure one should use $(a).unbind('click'), $(a).die(), $(a).removeAttr('onClick') – jAndy Apr 14 '10 at 07:02
  • In jQuery >= 1.7, you should use `.off()` instead of `.unbind()` – developius Jan 27 '16 at 19:27
-5

AFAIK this won't be possible, since page redirection can occur in many ways. He can click on an anchor tag, can submit a form with different action, set window.location to another page on button click and invoke server code to redirection.

And you won't be able to detect many of these.

rahul
  • 184,426
  • 49
  • 232
  • 263