1

I dont know if this is possible because I'm pretty new to JSON and jQuery. These are my two scripts:

$.getJSON('http://shop.com/test/?format=json', function(data){
  $.each(data.textpage, function(index, text){
    $('<div></div>').html('' + text + '').appendTo('.verzendkosten');
  });
});

and

$.getJSON('http://shop.com/vraag/?format=json', function(data){
  $.each(data.textpage, function(index, text){
    $('<div></div>').html('' + text + '').appendTo('.eigenschappen');
  });
});

Is it possible to combine these two? Both scripts work but I'm curious how or if these can be combined.

2
  • can you clarify what you mean by "combine"? Commented May 28, 2012 at 2:21
  • What exactly do you mean by "combine"? You are making two requests to two different URLs. You cannot make one request out of this. And JSON does not seem to be of any relevance here. Commented May 28, 2012 at 2:21

4 Answers 4

1

Not sure what you mean by combining. I am assuming you would like to get rid of the duplcate code in both the snippets.

Wrap this into a generic re-usable function and pass the page name and class name as parameter

function DoSomethingGreat(page,targetClass)
{
   $.getJSON('http://shop.com/'+page+'/?format=json', function(data){
     $.each(data.textpage, function(index, text){
       $('<div></div>').html('' + text + '').appendTo(targetClass);
     });
   });
}

and Call it like

DoSomethingGreat('test','.verzendkosten')

and

DoSomethingGreat('vraag','.eigenschappen')

wherever applicable

EDIT : As per the comment,

If you want to execute these when some page loads, Use the jQuery dom ready function.

Put this in the page where you want to call it

$(function(){
   DoSomethingGreat('vraag','.eigenschappen')
});
Sign up to request clarification or add additional context in comments.

11 Comments

What I mean with combining is indeed the duplicate code! I was wondering that when both functions "look" the same it should be possible to combine those.
@ Shyju: I'm trying this one out and looks like the best working solution provided. But how do I call the function on the specific page? Probably a silly question :( I've places the code in a "main.js" file. But how do I fire the function on different pages?
bind it to your controls. When do you want to invoke this function ? On a button click ? dropdown option change ?
No just when the page is opened! For example when I open a product page then DoSomethingGreat('test','.verzendkosten') should be placed in a tabpage called shipping costs. When I open eg. a FAQ page then DoSomethingGreat('vraag','.eigenschappen') should be displayed. And so on... What exactly do you mean with "bind it to your controls"? Do you have an example maybe? Anyway thanks for helping me out, you already gave me lots of interesting information!
@JaapVermoolen: Use Dom ready event. Check my updated answer.
|
0

Have a look at http://api.jquery.com/jQuery.when/

You will be able to use their example to achieve this, something like:

$.when(
    $.getJSON('http://shop.com/test/?format=json'),
    $.getJSON('http://shop.com/vraag/?format=json')
).then(function () {
    // Do stuff with data
});

the function defined with .then() will execute once both the getJSON calls have completed.

This is assuming you want to 'combine' the data returned from both

1 Comment

I will give this a try! Thanks for your answer and I will let you know if this worked
0

If your server can combine both responses into one, then yes. Otherwise, you would need to use the Deferred feature in jQuery.

The response could be like this:

{"data": {
    "kosten": "whatever you would have returned from /test/",
    "eigenschappen": "whatever you would have returned from /vraag/"
}}

Then your JavaScript can access those:

$.each(data.kosten.textpage, ...)

$.each(data.eigenschappen.textpage, ...)

Comments

0

Okay, combining the scripts may be done in a number of different ways. The most simply solution being to make the variable part of the request url really variable in the script.

It does, however depend on what you're looking for as a result.

1 Comment

The solution I was going to suggest is the one Shyju pasted below. Saves me some time anyway ;) Good luck mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.