Click here to Skip to main content
15,903,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this code in my master page for disabling back button in browser but how does this code work can some one explain. It works fine but i don't know how?


XML
<script type="text/javascript">

        function changeHashOnLoad() {
            window.location.href += "#";
            setTimeout("changeHashAgain()", "50");
        }
        function changeHashAgain() {
            window.location.href += "1";
        }
        var storedHash = window.location.hash;
        window.setInterval(function () {
            if (window.location.hash != storedHash)
            { window.location.hash = storedHash; }
        }, 50);

</script>


and i have called the function here in <body>
<body onload ="changeHashOnLoad();">
Posted
Comments
Sergey Alexandrovich Kryukov 15-Jan-15 1:50am    
Wrong question and a bad approach to learning programming. How could we figure out what are your concerns about this code? You did not even explain what did you want to achieve and how, wha'ts you "working" means. It's more efficient to write code by yourself using documentation. Don't write what you don't understand, proceed slower. Slower is ultimately faster.
—SA
Sinisa Hajnal 15-Jan-15 2:17am    
Wherever you copied that from should have some explanation on how the author did it and why.
Parth Mashroo 15-Jan-15 3:16am    
Sorry Sergey Alaxandrovich sir i am actually new to this and learning coding so i had a requirement of disabling back button on logout so i googled it and found this code and it was working so i tried it but was not knowing how it works so i asked this question sorry for not explaining it in a better way

SIr if you have better suggestion on this then please help me write a better code and once again thanx for reply!!
Parth Mashroo 15-Jan-15 6:06am    
Sir i have replaced the code and added if condition which checks session on pageload if it is null the redirect it to homepage otherwise continue.

1 solution

This code do one of the most harmful things you can imagine!
1. Put your page in the browser history at every 50 ms, that will create the feel back button does not work as you are traveling back to your own page, that loads from the browser cache...
2. You abuse the basic right of the end user to travel between sites as wishes! Very good way to create yourself a bad reputation!
I give you two advises...
1. Do not use this code!
2. Do not involve yourself with the browser, your concern should be your application and not the tool/environment the end user took to visit it!
 
Share this answer
 
Comments
Parth Mashroo 15-Jan-15 3:10am    
Sir i understood what you are saying then do you have any suggestion on doing it a better way,i am disabling back button on browser after logout so i googled it and found this code that was working but it seems that it is harmfull according to your explanation then sir can you help me write a better code and thank you for reply

I am actually new to this so i just tried the code and it worked so i used it but was not knowing what it does so i asked here again thanks for explanation!!!
Kornfeld Eliyahu Peter 15-Jan-15 3:14am    
Please read my response carefully again!
I stated that playing around with user's browser is not your job! You should not do that. It's the users personal property and he will decide what to do and where and when and not you!
I believe you have some problem with your application that you try to solve in a twisted way - may be better to present your original problem to see if someone experience has an idea for you...
Parth Mashroo 15-Jan-15 3:24am    
Yes Sir I will explain the whole scenario see
1.HOmePage That contains Login form after login it is redirected to products page where the user can add products and view them in grid view and have also used session for displaying particular data to particular user and also have a logout button after clicking logout button the user is redirected to homepage and there i have disabled the back button so that user dosent go to products page i have also cleared sessions on logout i just wanted to disable the back button thats it i have explained full scenario.

and thanx for your quick reply
TheRealSteveJudge 15-Jan-15 4:06am    
Agree! 5*
Kornfeld Eliyahu Peter 15-Jan-15 7:34am    
Thank you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900