tags:

views:

38

answers:

1

It's required to partial upload Html page with $(selector).load('/sitePath', params, function() { ... }); method. Html layout comes ok but it would be nice to return some additional values for javascript also.

Nowadays I return special javascript block with javascript variables inside html layout to read their values on load callback. What is the best practice to perform such operation?

Thank you in advance!

+1  A: 

I'd go for JSON and .ajax().

$.ajax({
   url:      '/sitePath',
   dataType: 'json',
   type:     'POST',
   data:     params,
   success:  function(data){
       $(selector).html(data.newcontent);
       var info = data.additionalinfo;
   }
});

Of course assumes that you create a JSON string on your server that helds both, a property named newcontent which should contain html markup and a property named additionalinfo.

jAndy
Thank you! But is it possible to refactor this to load method?
Andrew Florko
@Andrew: unfortunatly not, `.load()` is a wrapper for `.ajax()` which automatically loads the received content into the selected element. That is the purpose of this method. To have more controll you need to deal with the underlying `.ajax()` method.
jAndy
It's so pity. I have to use classic ASP.NET (not MVC) application in a project and use aspx page to createpartially loaded html layout. This page heavily relies on asp: controls. That's why I hesitate about layout to json conversions.
Andrew Florko
@Andrew: well, another way to receive some kind of `data` among the actually html/markup is to send a second request. One with `.load()` and another `.get()` for instance to receive data. Not that elegant tho.
jAndy
Ok, thank you, @jAndy
Andrew Florko