Resolved Using Javascript on UIWebView to get all of a particular div class

Discussion in 'iOS Programming' started by troop231, Apr 2, 2013.

  1. troop231, Apr 2, 2013
    Last edited: Apr 2, 2013

    troop231 macrumors 603

    Joined:
    Jan 20, 2010
    #1
    I'm trying to put the results of all the div classes named "thumb" of a finished UIWebView into one string.
    I appreciate your help!

    EDIT: Working Code (Problem solved):

    Code:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.thewebsite.com"]]];
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView
    {   
        if (!webView.isLoading)
        {
            NSString *output = [self.webView stringByEvaluatingJavaScriptFromString:@"function f()"
                                    "{"
                                    "var theThumbs = document.getElementsByClassName('thumb');"
                                    "var string = '';"
                                    "for(var i=0; i < theThumbs.length; i++)"
                                    "{"
                                    "string +=(theThumbs[i].innerHTML);"
                                    "}"
                                    "return string;"
                                    "}f()"];
    
            NSLog(@"String is: %@", output);
        }
    }
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What do you think return() does inside your for loop (inside your function)?
     
  3. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #3
    Does it stop the loop?
     
  4. dejo, Apr 2, 2013
    Last edited: Apr 2, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Sorry, but I'd prefer if you didn't answer my question with a question. The reason I asked is that I'm trying to get you to look critically at the code you've written. What functionality do you think that return() line provides at that point in the code? How do you think it solves the problem you're attempting to use it for? Etc, etc...
     
  5. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #5
    Nevermind.. I see what I did.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  7. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    It might be overkill, but adding JQuery to your project would make this trivial.

    JQuery makes a lot of JS trivial so if you're not familiar with it already and you're planning on writing more JS code, becoming familiar with it may be worthwhile.
     
  8. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #8
    Thanks for the information ArtOfWarfare! I have no plans to write any more JS code in this project, but if I need it in a future project, that's what I will use!
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    If a thread is marked resolved someone reading it should be able to figure out how it was resolved.

    Um, what was the fix?
     
  10. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #10
    Look in my original post..

     

Share This Page