0

**First of all, i'm sorry for my bad english!

I am trying to write a userscript for some website. This website updates himself every click or some seconds. (it uses ajax to update the view without refreshing) My userscript have to append some html elements when the user is in certain view. How can i run my code every view change? (after ajax)

The page have a function called ajaxCall that sends an ajax request and changes the page according to the response. I tried something like this:

var source = window.ajaxCall;
window.ajaxCall = function(param){ 
    source(param);
    myFunc();
}

this didn't work because ajaxCall sends async ajax request, so: first, the source function called (ajax request started), and immediately after it, my function called. (before the ajax request succeed and view changed)

Is there a way to run my code immediately after page updates?

Thank you very much.

user2153436
  • 81
  • 1
  • 1
  • 7

1 Answers1

1

try to use JQuery Library

Example 1:


 $.ajax({
  url: "http://fiddle.jshell.net/favicon.png",
  beforeSend: function( xhr ) {
    xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  }
})
  .done(function( data ) {
    if ( console && console.log ) {
      console.log( "Sample of data:", data.slice( 0, 100 ) );
    }
  });

example 2:

    var jqxhr = $.ajax( "example.php" )
      .done(function() {
        alert( "success" );
      })
      .fail(function() {
        alert( "error" );
      })
      .always(function() {
        alert( "complete" );
      });

    // Perform other work here ...

    // Set another completion function for the request above
    jqxhr.always(function() {
      alert( "second complete" );
    });
Hamza Alayed
  • 635
  • 5
  • 17
  • 1
    I'm not sending the request, the page himself does. I'm just writing a userscript for this page, so i cant do this. – user2153436 Apr 21 '15 at 05:47
  • `$(document).ready(function(){ $.ajax({ url: "http://fiddle.jshell.net/favicon.png", beforeSend: function( xhr ) { xhr.overrideMimeType( "text/plain; charset=x-user-defined" ); } }) .done(function( data ) { if ( console && console.log ) { console.log( "Sample of data:", data.slice( 0, 100 ) ); } }); });` – Hamza Alayed Apr 21 '15 at 05:49
  • The `ajaxCall` function sends the request. I cannot edit the function code. – user2153436 Apr 21 '15 at 05:52