0

Is there anyway to call a function when for example pressing a close button on a modal window that will take different action depending on the function that opened the modal window?

So say for example we had a landing page with items to click on that showed a image of that item in a modal window and a certain function was called when the image was opened from this context and we had a search side nav-bar that displayed items and when these were clicked the function that opened the modal windows was different from the first. Now when closing the modal window, and depending on the function that was called to open the modal, I would like to write a condition that would allow me to either go back to landing page or return to side nav-bar.

I don't have any code to show, but I was wondering if such a thing is possible; writing a condition based on the function that was previously called? What would be the command for that condition?

So

function 1 () {
doSomething;
}

function 2 () {
doAnotherThing;
}

$("closeButton").on('click', function () {
if (function 1 was called) {
// do something else
} else if (function2 was called) {
// do another thing
}
}

Could something like that be possible?

3
  • Pass identifying arguments to the function and branch based on the arguments, not who called it. It is not a good idea to write code that behaves differently depending upon who called it - it's a very hidden contract and just not a good idea. It is technically possible in Javascript if you don't use strict mode, but I've never seen anyone think it was a good practice. On the other hand, passing arguments to your function that affect it's behavior is probably the most common design pattern in programming. Commented Nov 25, 2014 at 23:11
  • What kind of modal are you using? Commented Nov 25, 2014 at 23:12
  • Ok. So are you saying that using context, for example, instead of the actual function? Commented Nov 25, 2014 at 23:13

5 Answers 5

2
var fnClicked = null

function fn1() {
  fnClicked = fn1;
  doSomething();
}

function fn2() {
  fnClicked = fn2;
  doAnotherThing();
}

$('closeButton').on('click', function(){
  if (fnClicked === fn1) {
    //do something else
  } else if (fnClicked === fn2) {
    //do another thing
  }
});

Alternatively you could hav fn1 and fn2 unbind the closebutton click event and rebind it to the appropriate followup.

Sign up to request clarification or add additional context in comments.

Comments

2

In an MVC framework, you can bind a property to the related view. If not, you can always keep bind state to the window object.

If you also don't want to do that, you can keep the state in the DOM (the close button) as an attribute. For example, a data-attribute.

$("closeButton").on('click', function (e) { 
    var state = $(e.currentTarget).data("state"); 
}

Comments

1

You can use data attributes on the modal element to store info that indicates what area the modal was opened from. Then when closing the modal, look in that attribute and decide what to do based on the value stored there when the modal was opened.

Comments

1

Variables can store references to functions in Javascript. So I would have function1 set some internal variable that would be checked when you close the modal:

var calledBy;

function1 () {
  calledBy = function1;
  //open modal
}

function2 () {
  calledBy = function2;
  //open modal
}

$("closeButton").on("click", function () {
  if(calledBy === function1) {
  //...
  } else if(calledBy === function2) {
    //...
  }
});

But as hyperstack pointed out, it's better organization to have one function for opening the modal and pass in an argument. I would have an object for the modal:

var modal = {
  //...
  calledBy: null,
  open: functio (calledBy) {
    this.calledBy = calledBy;
  }
};

Comments

0

You can use the 'this' special keyword to refer to the object on which a method is being invoked.

EG.

<div class="cval">
    test
</div>

<script>    
        $(".cval").click(function (e) {

            e.preventDefault();
            alert($(this).attr('class'));
            if($(this).attr('class') == 'cval')
             //dosomething
            else
             //dosomething
        });
</script>

Interrogating any of the elements attribute(s) for value and then using a conditional to control flow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.