-3

I have a javascript code snippet at the end of my pages. The code works on page load, but I wish to fire this code after an ajax request as well as on page load. The javascript code is as follows:

(function(d, t) {
        var g = d.createElement(t), s = d.getElementsByTagName(t)[0];
        g.src = '//api.reftagger.com/v2/reftagger.js';
        s.parentNode.insertBefore(g, s);
    }(document, 'script'));

I moved this function inside the ajax .get call of my plugin and now it works successfully on ajax calls. However I still need it be included at the end of the page to make it work without ajax as well.

How can I include both without making one cancel out the other?

EDIT: I fixed using a conditional statement to exclude on ajax pages. Thanks everybody!

  • 2
    what does your ajax call look like? are you using a framework like jQuery? There is definitely a way to hook into ajax response event, but the syntax varies depending on how you're actually making the ajax call – CrayonViolent Feb 22 '14 at 21:10
  • Isn't that an anonymous function? To call it in multiple places, I believe you'd have to make it a named function, and call the function from within the appropriate event bindings (doc onload, ajax success, etc) – random_user_name Feb 22 '14 at 21:12
  • @CrayonViolent : assuming `jQuery`, without pasting the code into the `jQuery.ajax()` success (or other) callback, how would you attach another function call to the `jQuery.ajax()` response? Link or fiddle would be great. I am not aware that you can do this. – Dmitriy Khaykin Feb 22 '14 at 21:28
  • @DavidKhaykin well I wasn't really shooting to get into specifics until he posted details about how he's actually implementing it, but you can in fact attach a callback to jquery ajax calls without specifying a success function in the actual call: http://api.jquery.com/category/ajax/global-ajax-event-handlers/ – CrayonViolent Feb 22 '14 at 21:37
  • @CrayonViolent: I asked for selfish, personal reasons related to taking over the world. Hehehe. Ok actually it's to improve an answer I posted the other day :) – Dmitriy Khaykin Feb 22 '14 at 21:38
  • 1
    @DavidKhaykin under the hood jQuery basically prototypes the `XMLHttpRequest` object to attach success callback function, be it in the individual `.ajax` `.get` `.post` calls or with their global callback methods (from link I posted). So even if jQuery isn't being used, you can do the same thing with "regular" javascript. I asked a question and posted an answer to that some time ago, but for tapping into the request methods (not the response) IOW same principle, diff ajax event. http://stackoverflow.com/questions/3596583/javascript-detect-an-ajax-event if you're interested – CrayonViolent Feb 22 '14 at 21:51
  • @CrayonViolent: Always interested to learn something new. I will check this out. Your help is much appreciated. – Dmitriy Khaykin Feb 22 '14 at 21:57
  • @CrayonViolent Could you point in the right direction based on the syntax of the script? – user3341772 Feb 22 '14 at 23:47
  • @user3341772 if you want the code to execute whenever *any* ajax request is made, then just wrap your code in a `$.ajaxSend()` `$.ajaxComplete()` or `$.ajaxSuccess()` (follow the first link I posted for syntax) and put it at the bottom of your page or within a `$(document).ready(..)` or whatever. Which one you use depends on when you want it triggered.. do you want it triggered whenever an ajax request is attempted? Or when it's complete? or when it's successfully completed? Read the doc descriptions from the link and decide. – CrayonViolent Feb 23 '14 at 01:52
  • But you want it to only execute when a *specific* ajax event happens.. well, I see several in your code (a `$.get` `$.post` and `$.ajax` call) so you'll have to be more specific about which one(s) you want it to trigger on. But if you go to the jQuery doc entries for `.get` `.post` or `.ajax` you will see that one of the arguments for all 3 of those is to specify a callback function. – CrayonViolent Feb 23 '14 at 01:56
  • So basically you need to decide a) *what* ajax event(s) on your page you want that other code to execute on (you have 3 in your posted code), and b) *at what point* in the ajax call(s) you want the code to trigger. Come up with an answer to those 2 things and then I (or someone) can give you a relevant code example. Though I encourage you to try your own hand it it based on the info I have provided, and then ask a question if you get stuck on one of them in particular – CrayonViolent Feb 23 '14 at 01:58
  • Thanks, I think I should be able to find my way with your help here. I appreciate it a ton! – user3341772 Feb 23 '14 at 02:19
  • @CrayonViolent: Thanks again for pointing me to the jQuery stuff; it was useful. Essentially it's the best I think that can be done. I've updated the answer in question mentioned earlier (unrelated to here) to use ajaxComplete, if you want to check it out: http://stackoverflow.com/a/21926988/546000 – Dmitriy Khaykin Feb 23 '14 at 05:55

1 Answers1

0

Simply copy the content of your function to your ajax callback function and then remove the function from end of your page

semirturgay
  • 4,151
  • 3
  • 30
  • 50
  • I found out where to put the function and it works just like that. However I still need it to work without ajax as well. How could I make both work without having a conflict? – user3341772 Feb 23 '14 at 09:02
  • update your question and share your code so i can help you.without seeing your code i cannot say anything – semirturgay Feb 23 '14 at 11:56