Jump to content

$.ajax( ) Strategy Question


iwato

Recommended Posts

BACKGROUND:  I now know how to produce dynamic HTML populated with the values of Javascript variables obtained from a database and processed with PHP.  Unfortunately, this method of integrating Javascript and HTML is somewhat awkward.  What I would much rather be able to do is to read the variables into a dynamically produce <script> tag and then manipulate them on the importing page. 

REQUEST:  To this end, please comment on the veracity and accuracy of the following statements:

  1. When an AJAX call is made only the Javascript contained in the success and complete functions is executed.
  2. It makes no difference that the HTML with which the Javascript interacts is already on the page when the AJAX call is made.
  3. Unless the AJAX call is made while the page is loading, all Javascript already contained within the page will have been executed before that contained within the AJAX statement. 
  4. Javascript variables imported via AJAX can be accessed by other Javascript on the same page after the variables have been imported.

Please advise.

Roddy

Link to comment
Share on other sites

1. Success and complete are just a jQuery thing, but if you're using jQuery that is correct. Those functions get called once the request is complete. In the case of plain AJAX, the readystatechange event handler is what's executed when the request is done.

2. The HTML must already be loaded if Javascript should interact with it, but that usually is the case since AJAX is usually called after the page has finished loading. Note that if you change the DOM using innerHTML or jQuery's html() method any references to HTML elements from that page region that were stored in variables will no longer work even if they have the same ID or class name as before. You will need to load them again using a jQuery selector or DOM methods such as getElementById().

3. That's correct.

4. AJAX doesn't import Javascript variables. It can return data that may be interpreted as a Javascript object. jQuery will automatically parse JSON for you into an object, but that object is only accessible as a parameter in the complete() method of the AJAX request.

  • Thanks 1
Link to comment
Share on other sites

Good morning, Ingolme!

Thank you for responding to my statements.  I believe that your responses will help me in avoiding certain kinds of pitfalls.  This said, I would like to elaborate a little more on Item #4.  What i have in mind is the following:

1) Use AJAX to retrieve data

2) Use the $.ajax() success function to assign the data to javascript variables included in a dynamically generated <script> tag.

3) Access those Javascript variables at will.

Please consider the following sketch and comment on its feasibility.

The HTML:  (flexible manual placement of HTML independent of the configuration of the AJAX success function)

<div id='js_script'></div>
<div>
	<p id='pod_num1'>Podcast No.: </p>
	<p id='pod_num2'>Podcast No.: </p>
</div>

The jQUERY READY() FUNCTION: (flexible variable placement independent of the configuration of the AJAX success function)

$(document).ready(function() {
	$('#pod_num1').append(var1);
	$('#pod_num2').append(var2);
});

 

The AJAX: (automatically generated variables on call)

$('#some_trigger').on('click', {podType:$(this).attr(id)}, function(event) {
  event.preventDefault();
  podtype = event.data.podType;
  $.ajax({
    url: 'some_php_file.php',
    method: 'GET',
    data: {'podType', podtype},
    dataType: 'json',
    success: function(jsonData) { 
        $.each(jsonData, function(key, object){ 
            $('#js_script').append('<script>var var1 = object.varname1; var var2 = object.varname2;</script>');
        });
    }
  });
});

 

 

Edited by iwato
Link to comment
Share on other sites

You could do that, that should make those variables available in the global scope, but that's kind of a roundabout way to do that, to use Javascript to append a script block.  Javascript is already running, you can just set things directly.  I mentioned this in another topic, but if you start by declaring those variables in the global scope then you can just set them directly later.  So put a script block on the page and define those variables when the page starts, and then in the ajax callback just set the variables directly instead of writing a new script block.  It just seems like a roundabout way to have Javascript write a script block when you can do things in Javascript directly, it's already running.

Instead of global variables, you might also consider using sessionStorage or localStorage to store things like that.

  • Thanks 1
Link to comment
Share on other sites

Yes, I understand.  I will write the <script> element directly into the page.  It does make better sense for my ultimate formatting goal.  In this way, the only job of AJAX is to send the data needed to obtain the data.  in this way the actual use of the data is a separate matter.

Yes, I now no how to implement the $_SESSION superglobal, but I will save its further use for other occasion.  Soon I must start putting the pieces together and am reluctant to add any more large pieces for the moment. 

Roddy

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...