Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Using .load()

  1. #1
    gfb
    gfb is offline
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using .load()

    Hey there. I haven't used this command before and was wondering what exactly you are selecting infront of .load(). What I am trying to do is get a list of usernames and put them in an array when a button is clicked. I have something like this:
    Code:
    $(".my-button").one("click", function(){
      var myArray = [];
      $(".member-list-container").each(function(){
         myArray.push($(this).html());
      });
    });
    However, .member-list-container is not on the same page as the button so I would like to use .load() to get the username list. What would it look like in this situation?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    .load() simply waits until the page finishes loading completely before running the associated code. You'd use it if you need the script to wait for all the images to finish loading before the script runs. Everything needs to be in the same page - otherwise you need to pass information from the first page to the second using either a cookie/local storage or via a querystring on the URL.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,629
    Thanks
    6
    Thanked 1,002 Times in 975 Posts
    I have to correct you on that, felgall: There are two different implementations of the load function. One is as you described: do something after the element has loaded (most likely an image). The other is a variant of the $.ajax() function that loads content from another document asynchronously.

    OP: The load function loads content into the element to which it is “attached”. I. e. $('#example').load(…) would load the external into the element with ID “example”.

    So, you should construct a document fragment and append that to the content once everything is done:
    Code:
    var myArray = [];
    var fragment = $('<div>').load('your_page.html', function() {
      $(".member-list-container").each(function(){
         myArray.push($(this).html());
      });
    });
    I don’t know what you’re doing with that array. Possibly you can even spare that.

  • #4
    gfb
    gfb is offline
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have something set up so that it takes an array of usernames and adds something based on what they put in a text box. The text box is on a settings page, a completely different page from the list of usernames. I was hoping to have one button do it all. Since that button also has to save other settings, it has to refresh the page. When that happens, I've lost my information haven't I? I didn't think about that until I tried it.

  • #5
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,629
    Thanks
    6
    Thanked 1,002 Times in 975 Posts
    Without more detailed information it’s hard to give you any useful advice. The way you described it is too general. If you don’t want to disclose any indentifying information then use a demonstration set-up.

    If what I posted above doesn’t help then it’s probably better to move this thread to the “AJAX & Design” forum because the concept of exchange of information is much more fundamental than a specific question about a certain function.

  • #6
    gfb
    gfb is offline
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Essentially I need to get the usernames listed here(or similar member lists on the site): http://battlelog.battlefield.com/bf3...8/listmembers/ and put them in an array, but only doing so once and from a button on another page.
    The setup looks like this right now:
    Code:
    var myArray = [];
    $(".base-button-arrow-small").one("click",function(){
    var fragment = $("div").load("http://battlelog.battlefield.com/bf3/platoon/2832655241118731688/listmembers/", function(){
      $("..platoon-member-memberslist-user-username").each(function(){
        myArray.push($(this).html());
      });
    });
    Go ahead and put the thread where you see fit.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •