1

I have 2 methods I want to execute when button or link is clicked One is a Javascript that will send information to a 3rd party and the 2nd one is a backend Django method that will update a field in the database. This way I can control that JavaScript Function will execute only once.

Ideally, Javascript should execute first and only after its execution the backend method will be triggered

I tried to combine it in the same link but this is wrong since only javascript executes

<a href="{% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %}" onclick="AddCardToTrello()">Add to Trello</a>

What are my options here ? Can I call my backend method from JavaScript?

UPDATE:

Despite all the great answers the tricky thing in my case how the backed call is generated {% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %}

I use Django as my backend and probably there some difference between invoking the backend method from within JavaScript vs direct click. What happens is that when it is called from within JS domain name gets cut off.

window.location.href = /production/production/order/board/5/127.0.0.1:8000/production/soproduct/details/5/

When the real link is

http://127.0.0.1:8000/production/production/order/board/5/http://127.0.0.1:8000/production/soproduct/details/5//

So somehow when I call the method from Javascript it cuts off my first part of URL(domain name) and this breaks the JS script probably. So none of the answers works. I am not sure about the Ajax since was not able to make it to work so far.

Ilya Bibik
  • 3,924
  • 4
  • 23
  • 48
  • You can call backend function in AddCardToTrello() – vaqifrv Jul 28 '16 at 13:49
  • 1
    add one function to call both functions. isn't that possible? – ClearBoth Jul 28 '16 at 13:49
  • Possible duplicate of [How to call multiple JavaScript functions in onclick event?](http://stackoverflow.com/questions/3910736/how-to-call-multiple-javascript-functions-in-onclick-event) – Manish Jul 28 '16 at 13:50
  • Wait, you do not want to call javascript methods? You want a single link to follow a link and also call the function? – epascarello Jul 28 '16 at 13:51
  • You should fix your question title. All of the current answers seem to be missing the point. – Brett Jul 28 '16 at 14:06

5 Answers5

4
AddCardToTrello(event) {  
  event.preventDefault(); //to stop the page from redirecting
  doStuff(); // do the stuff you to do first
  var href=$(this).attr('href');  
  window.location = href; // redirect here 
}
Brett
  • 4,268
  • 1
  • 13
  • 28
michael berezin
  • 1,146
  • 7
  • 8
2

To expand @TrampolineTales answer, since one of your two actions is a navigation to a different page, you can

function addCardThenGo(url) {
    AddCardToTrello();
    window.location.href = url;
}

then

<a onclick="addCardThenGo({% url 'add_to_production_board' pk=soproduct.id uri=request.build_absolute_uri %})">Add to Trello</a>
alebianco
  • 2,475
  • 18
  • 25
  • When I use this snippet url is not created correctly , it is probably related how my Django framework builds url from click vs. from calling it within function window.location.href = /production/production/order/board/5/http://127.0.0.1:8000/production/soproduct/details/5//; – Ilya Bibik Jul 28 '16 at 14:02
  • Well I don't know django specifically, but isn't that just a problem with the add_to_production_board method that should return a valid url? In your case I'd guess it returns an absolute url without the protocol, which is interpreted as relative – alebianco Jul 28 '16 at 21:38
  • Not really since when I do not return it within JS is returns the correct url. Weird .... – Ilya Bibik Jul 29 '16 at 00:19
  • http://stackoverflow.com/questions/38684815/how-to-execute-django-uri-template-tags-from-within-client-side-java-script?answertab=oldest#tab-top Working solution – Ilya Bibik Jul 31 '16 at 15:02
1

You could simply create a function which contains the two functions you wish to be executed.

function toBeExecuted() {
   f1();
   f2();
}
TrampolineTales
  • 808
  • 3
  • 14
  • 31
  • exactly. Just add a `wrapper` function for both functions and call that `wrapper`. – Jorrex Jul 28 '16 at 13:51
  • sorry, for stupid question doest it matter in any way that my 2nd method is not JavaScript Function? – Ilya Bibik Jul 28 '16 at 13:54
  • @BorisTheAnimal Yes it matters. Look up how to call a server-side function from the client. Hint: Ajax (if you do not want to refresh the current page). – Brett Jul 28 '16 at 14:00
1

it turns out you have two questions. One about calling two functions and a second question how to "call a function on the backend"

calling two functions

You can use what others have pointed out (one function, that calls the other two). If you don't want that you could just register two event handlers

Register two handlers

$(function(){
   $(".demo").click(function(){
        console.log("handler1");
     });
  $(".demo").click(function(){
        console.log("handler2");
     });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="demo" >Add to Trello</a>

obstructive

If you wanted to use obstructive javascript (not recommended) you can do the following

function handler1(){
  console.log("handler 1");
}

function handler2(){
  console.log("handler 2");
}
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a class="demo" onclick="handler1();handler2()" >Add to Trello</a>

Calling "backend code"

While this is not really an answer to your "second" question, i did add it to give you some direction. Your question is also vert broad. I t depends on a lot of things (for example what platform/language you use on the "back end"). Look in the comments and other posts for some valuable resources on this topic.

You will need to expose your "function" on the server ("backend") through http. This exposes your "function" as a http-service to clients. Your client (the browser) can call the http service using XHR. Below is some skeleton code that uses jquery.

function handler1(){
    // there are many other methods like $.get $.getJSON
    $.ajax({
       type: 'GET',
       dataType: 'json',
       url: "http://piedpiper.com/api/callers"
    }).then(function(result) {
        // do something with the result

    });
}
raphaëλ
  • 6,393
  • 2
  • 29
  • 35
1

A lot of the answers so far seem to be missing the point. Unless I'm completely misinterpreting your question, they are only partly correct. Yes, you can call a back-end function from the front-end, but you need to use Ajax on the front-end and your back-end function needs to be configured to accept an HTTP POST request.

This answer assumes you are using jQuery. Also, it's unclear what the name of your back-end function is or how your server is configured. I'm making a lot of assumptions here. You should provide more information.

function callBackEnd(url) {
  $.post(url, function() {
    console.log('HTTP POST sent');
  });
}

function AddCardToTrello() {
  // ...do whatever this does
  // call the backend
  callBackEnd('http://www.yourserver.com/backend/function');
}
Brett
  • 4,268
  • 1
  • 13
  • 28
  • My backend function is Django framework method and this is link I am building to call it it is shown in the link in body of the question And yes jQuery is included in my template – Ilya Bibik Jul 28 '16 at 14:22