-1

How to use following javascript code in Objective-C ?

var theFormId = $('form').filter(function() {
return this.innerHTML.indexOf('forgot your password') != -1;
}).attr('id');

HOw to use code above in UIWebView's stringByEvaluatingJavaScriptFromString: method ?

I used the code above with Objective-C as follows:

NSString *formID = [NSString stringWithFormat:@"%@",[self.browser stringByEvaluatingJavaScriptFromString:@"$('form').filter (function() {var text = this.innerHTML.toLowerCase();" 
                        "return text.indexOf('forgot your password') != -1 || "
                        "text.indexOf('remember me') != -1;}).attr('id');"] ];

When trying to log formID , it prints nothing. Is there any syntax error in my code ?

2
  • Please refer to the related question [here][1] this will help you a lot [1]: stackoverflow.com/questions/766627/… Commented Apr 9, 2012 at 8:09
  • 1
    @AalokParikh to use links in comments you need to use this format [text](http://URL) Commented Apr 10, 2012 at 14:44

1 Answer 1

2

I have no idea if this will work because you have only given a very small part of your problem.

Firstly, your Javascript doesn't look like it is valid. I suspect it should be:

var theFormId = $('form').filter(function() {
    var text = this.innerHTML.toLowerCase(); 

    return text.indexOf('forgot your password') != -1 || 
           text.indexOf('remember me') != -1;
});

I added some closing brackets.

To run this Javascript using -stringByEvaluatingJavaScriptFromString: you would do something like the following.

NSString *javascript = @"var theFormId = $('form').filter(function() { var text = this.innerHTML.toLowerCase(); return text.indexOf('forgot your password') != -1 || text.indexOf('remember me') != -1; });";

NSString *result = [webView stringByEvaluatingJavaScriptFromString:javascript];

I'm not sure what you'll get in the result string when you run this though.

Sign up to request clarification or add additional context in comments.

1 Comment

Please to go to my original question at here : stackoverflow.com/questions/10069564/…

Your Answer

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