Click here to Skip to main content
15,903,523 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi.. I wrote the below script in my master layout for browser refresh and close..
I need to log out the user in both scenarios and below function works well..

<script type="text/javascript">

    var refresh = true;
    var success = false;

    $(document).ready(function () {
        $("a,:input").click(function () {
            refresh = false;

        //Script for Browser refresh and close...
        var myEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';

        myEvent(chkevent, function (e) {
            if (refresh == true) {

                    type: "POST",
                    traditional: true,
                    dataType: "json",
                    async: false,
                    url: '@Url.Action("MyActionMethod", "MyController")',

                        success: function (data) {

                            if (data == "Success") {
                                success = true;



In my content view i have the anchor tag like follows.. and clicking the link it vl redirect to respected url.. But when I m cliking that link first it fires the refresh_close related function which exist in my master layout.. I don't want to fire that function when clicking the link..

This is my anchor

I need to restrict calling beforeunload event while cliking anchor..

How can I accomplish this..

Thanks in Advance..
Updated 6-Nov-14 17:38pm

1 solution

<a id="aTest" href="GoogleMapsAPITest.aspx" onclick="return MyFunction();">Test Here !</a>

function MyFunction() {
  //add logic here to conditionally return true or false
  // false means no reloading.
  return false;

Hope this makes sense for you.
Share this answer

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