0

I am trying to implement some simple javascript in my iOS app. I inject the javascript using the usual method

[webView stringByEvaluatingJavaScriptFromString:jsCode];

However, a script as simple as

$(document).ready(function(){
       alert(document.location);
       });

seems to work on some websites (for example www.nytimes.com), but not on others such as www.latimes.com. The function above is ridiculously simple and I expect it to work uniformly everywhere. Does anyone know a reason for this behavior?

Nikhil J Joshi
  • 1,177
  • 2
  • 12
  • 25
  • 3
    How exactly are you running this script on those sites? – JJJ Jul 21 '13 at 12:56
  • okay, I perhaps need to provide more information. Let me adjust the question itself – Nikhil J Joshi Jul 21 '13 at 12:56
  • any chance related to http://stackoverflow.com/questions/2430936/whats-the-difference-between-window-location-and-document-location-in-javascrip – drhanlau Jul 21 '13 at 13:01
  • I don't think I am me/issing with window or document locations here. – Nikhil J Joshi Jul 21 '13 at 13:03
  • 1
    From the console of a browser on a normal PC, your code snipper works on both www.nytimes.com and www.latimes.com for me (assuming I add jQuery to the page first, of course!). I trust you're including jQuery? Can you provide steps to reproduce the issue you're having? – Mark Amery Jul 21 '13 at 13:04
  • What do you get when it doesn't work ? `TypeError: Object # has no method 'ready'` ? – Akheloes Jul 21 '13 at 13:06
  • @MarkAmery: I usually check if the page already has jQuery or not, in the latter case I add it from the CDN. Now, I may be doing something wrong if I check existence of jQuery by the function `if(jQuery) return 1;` – Nikhil J Joshi Jul 21 '13 at 13:06
  • Okay, I dropped the jQuery check function and now add jQuery always... it works well now :)... I hope adding jQuery redundantly is not a performance-unfriendly. – Nikhil J Joshi Jul 21 '13 at 13:18

1 Answers1

0

I think it will depend on the existance of jQuery in the ressources of the site (scripts), otherwise I would resort to alert(window.location); which is pure JS and it will uniformally work fine.

Akheloes
  • 1,352
  • 3
  • 11
  • 28