I need to stop all the page redirection through javascript.
开发者_Go百科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.
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.
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
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.
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
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.
精彩评论