0

I wanna use $(window).resize to call some function when a mobile device orientation change, I wrote all my code inside the $(document).ready, this works when I use an Android device, but with iPhone after first orientation is detected it doesn't call $(window).resize even again. When I put the (window).resize function outside of $(document).ready, it also works on iPhone.

Inside this $(window).resize function, I have to make call to the methods which are defined in the $(document).ready block, so how can I do that?

Baris
  • 471
  • 6
  • 19
  • simple thoughts... cant you make the function global??? or public or something like that? – Mathlight Sep 03 '12 at 14:28
  • Just take an look at this, and that should be enough: http://stackoverflow.com/questions/6960448/global-jquery-function – Mathlight Sep 03 '12 at 14:31
  • 1
    You have to assign them to a variable outside `$(document).ready`. – Felix Kling Sep 03 '12 at 14:36
  • Could you post your code (a stripped-down version of it, please)? – bfavaretto Sep 03 '12 at 14:38
  • 1
    simple thoughts... cant you make the function global??? or public or something like that? Take an look at this: [http://stackoverflow.com/questions/6960448/global-jquery-function](http://stackoverflow.com/questions/6960448/global-jquery-function) – Mathlight Sep 03 '12 at 14:27
  • @TWCrap my confusion was I'm doing parsing an XML with JQuery in $(document).ready, so before document isn't ready I cannot do parsing. I've overcome the iPhone resize problem by not dooing another JQuery parsing in it. – Baris Sep 13 '12 at 08:26
  • @FelixKling see my comment previous comment – Baris Sep 13 '12 at 08:27

2 Answers2

2

As you need to use something from two different event handlers, you should move that something out of the event handler. You can't create the methods in either event handler, because you can't know if ready or resize will be triggered first.

You can for example create an object that holds the methods:

var commonMethods = {
  data: 42,
  someMethod: function() { alert(this.data); }
};

$(document).ready(function(){
  commonMethods.someMethod();
});

$(window).resize(function(){
  commonMethods.someMethod();
});
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • But the question implies the resize handler is being defined inside the ready handler, so it doesn't look like a scope problem. Anyway, we can't be sure without seeing the code. – bfavaretto Sep 03 '12 at 14:47
  • @bfavaretto: The resize handler is defined outside the ready handler, to work with iPhone. (Second sentence in the question.) – Guffa Sep 03 '12 at 14:50
  • Yes, I've seen that, and your answer does answer the question in the title. My point is, I believe it should work with the resize handler defined inside the ready handler. I can't see why it wouldn't. iPhone oddity, maybe? – bfavaretto Sep 03 '12 at 14:55
  • @bfavaretto: If you have the resize handler inside the ready handler, it can only handle resize event that happens after the ready event is triggered. (The resize handler would of course have to consider that the page might be only partially loaded to handle resize events before the ready event.) – Guffa Sep 03 '12 at 15:00
  • Thanks for clarifying @Guffa, I wasn't thinking about that scenario. – bfavaretto Sep 03 '12 at 15:55
  • @Guffa my confusion was I'm doing parsing an XML with JQuery in $(document).ready, so before document isn't ready I cannot do parsing. I've overcome the iPhone resize problem by not dooing another JQuery parsing in it. – Baris Sep 13 '12 at 08:28
1

Define your methods outside the $(document).ready() block.
Then only call them as you need them throughout the code. This way, you have access to them both from the $(document).ready() and the $(window).resize() blocks.

cassi.lup
  • 1,261
  • 7
  • 23
  • my confusion was I'm doing parsing an XML with JQuery in $(document).ready, so before document isn't ready I cannot do parsing. I've overcome the iPhone resize problem by not dooing another JQuery parsing in it. – Baris Sep 13 '12 at 08:27