0

I am doing a dirty form check. While doing that, if the form value changes I show the user a prompt asking if he/she wants to proceed. If yes, I want to navigate to the url of clicked link. My problem is that I am not able to retain the url.

$("a").click(function (e) {
        url = $(this).attr('href');
        if(str != $("form").serialize()){
            $('#prompt').show();
            e.preventDefault();
        }
    });

I am using the bootstrap dialog to show message that form has been changed. Now if the user still wants to leave page, how do I keep track of the url I need to forward.

<div class="modal-footer">  
            <a id="hide" class="btn">Stay on Page</a>
            <a id="navigate" class="btn" href="#">Leave Page</a>  
        </div>

$('#navigate').click(function () {
        console.log(url)
    });

I need a way to retain the value of url, when I close the dialog.

Hozefa
  • 1,676
  • 6
  • 22
  • 34

1 Answers1

0

This probably has something to do with with where url is defined. I made a quick js fiddle that shows two examples of it work and not working depending on where url is defined http://jsfiddle.net/zdgNS/4/.

This javascript should works as my examples shows:

(function() {
     var url;
    $('#link').click(function() {
        url = $(this).attr("href");
        return false;
    });

    $('#button').click(function() {
        console.log(url);
    });
})();

If you want to read more about javascript variable scope, read this question: What is the scope of variables in JavaScript?

Community
  • 1
  • 1
hajpoj
  • 13,299
  • 2
  • 46
  • 63
  • What I am looking for is that when I select leave page, I should have access to the url variable. Even if I can return back to the .click function, it would work for me. – Hozefa Apr 17 '13 at 17:33
  • Huh... I guess i don't quiet understand what you are going for. In the example I gave both `click` functions have access to the url variable. and whether the click function has access to the url variable has everything to do with where you define the url variable. – hajpoj Apr 19 '13 at 00:03