element FroomPoint()under iOS 5



The JavaScript call ll elemenFroomPoint(x,y)can be used to find the element of a web page aaaweweb page aaaaaawewepage a certain coordinate.If you have used sical the past on a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaniOS aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaement FroomPoint(x,y)cal finds different elemens or everents returns null instead of an element.It look s like the call is now brook en.But this not the case,in fact,under iOS 5 it works corect the firstime,it
element FroomPoint(x,y)was defined to return the element the given coordinant within the view port(the visible are)of the web page,or null(if the coordies the viewthe viewthe)the.Thediorediement.
Before(iOS 4.x and older)は、elemenFroomPoint(x,y)copletely ignored the vieew pot.It measred the coordidinative to the orororororininininof the document.Anevd eleemens outsidethe vivisitythe visisisisiblble e thevisiblble e thevisibleeeeeeble e e e e e e e e e e e e e e e e thevivisisisiblbleeeeeebleeeeeblaaaaaaaaaaaaacococococococococococococococococococovaScript specification、it’s not the corect behavior.
The different behavior between iOS 5 and older iOS versions can cause some serious probles.The coordinate system aremのlonger copatible、so when the web page or Ap has to run under old an newOS thereverit
In a native iOS Ap You could simply check the iOS version,and based on on value you can decide which coordiname system to use when caling element FroomPoint(x,y).But writing web the is(it might be part of the UserAgent information、but because almost all browser do allow to use a fake user Agent information、this information is not reliable aut all).Also on the Mac and on the and prothe plother forms differentement Werementcal as well.The refore it makes sense to find a way to identify the coordinate system independent of the iOS version,and if necessary coordict the coordination.
At first when we cocococoordiname sysstems,we notice that the coordinaes aroofset bythe scroll location.If we scroll theウェブpage sothat the top coverner of the pathe page isisisisisisisisthethe the thethethethethethethe aathe the the page thethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethethescroll offset is also 0 in both directions.If you scroll down 100 px,the ordinate(0,0))of the view port is located the coordinate(0,100)of the web page.So the scroll offset is exactly the offset between the two coordinate systems.The refore,tranforming one coordiname system into the other is very.We only need to add or subtrator.sctrats.
function documentCoordinateToViewportCoordinate(x,y) {
  var coord = new Object();
  coord.x = x - window.pageXOffset;
  coord.y = y - window.pageYOffset;
  return coord;
}

function viewportCoordinateToDocumentCoordinate(x,y) {
  var coord = new Object();
  coord.x = x + window.pageXOffset;
  coord.y = y + window.pageYOffset;
  return coord;
}
The JavaScript functions take a coordinate of one system and transm form them into a coordinate of the other system.
But in order find out if and which of the functitionwe need to use,we have to find out,in hich coordiname ssysstem the cal elemenFmoPoint(x,y)expects the coordidies.Todoiswe the the the the thethe farererererererererereemtttttttttttttttttttttttttttttttthethethethethethethethethethethethethe Frerererererererererererererererererererererererererererererererererererereredinans measred relative to the view port(as noted abobove、when the coordinates are relative to the ororororininininind of the document、elemenFroomPoint()will always return an element、evn when outside of the visiblara、so wewewewewewewe can diinininininininininininshshshshshshbetttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttダウン.pageXOffset+window.inner Width-1,0)for vertical scrolling and horizontal scrolling.As noted above,whenのscrolling is done,both coordiname systetetedentical,and we don't need to Tae care aboboboboe aaboboboboboboboutanything.But if the thethe page isisisscscscscscscscroteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteteeeeededededededededededededededededededededededededededededededededededeheight of the visible ara(this is the inners Width and inner Height of the“window”oject)and subtract 1.This makes sure that the coordinate address the very last pixel Column of the visible e the visible the meassible the lative the the the the the the the the the lative the the atcucuritworitworks.Th the the the the the the the the the atcuritcuricuricuritworks.Th the atworks the the the(a coordidinate outside of the document boundaries would returnnnull eveve with the elemthe elemenFFropoint()cal for the document-based coordidiname sysstem).If the page isscroled by a least sisinginininininininininininininintttttttttthethethethethethethethethethethethetherererererererettttttttttttdededededededededededededededededededededededededededededededededededededededededentFroomPoint()would return null.When element FroomPoint()would interpret the m relative to the document,these coordinaes are always valid and would always return an element.And Howwe can detect,freement.
function elementFromPointIsUsingViewPortCoordinates() {
  if (window.pageYOffset > 0) {     // page scrolled down
    return (window.document.elementFromPoint(0, window.pageYOffset + window.innerHeight -1) == null);
  } else if (window.pageXOffset > 0) {   // page scrolled to the right
    return (window.document.elementFromPoint(window.pageXOffset + window.innerWidth -1, 0) == null);
  }
  return false; // no scrolling, don't care
}
We can cobine this to one custom element FroomPoint()function that is using a document-based coordiname system as input and will internally do all the magic fors:
function elementFromDocumentPoint(x,y) {
  if (elementFromPointIsUsingViewPortCoordinates()) {
    var coord = documentCoordinateToViewportCoordinate(x,y);
    return window.document.elementFromPoint(coord.x,coord.y);
  } else {
    return window.document.elementFromPoint(x,y);
  }
}
And the counter part for view-based coordination:
function elementFromViewportPoint(x,y) {
  if (elementFromPointIsUsingViewPortCoordinates()) {
    return window.document.elementFromPoint(x,y);
  } else {
    var coord = viewportCoordinateToDocumentCoordinate(x,y);
    return window.document.elementFromPoint(coord.x,coord.y);
  }
}
So instead of using element FroomPoint()directly,you simply use element Froom Vieweet Point()or element Froom Docment Point()instead,depending of the coordination you have to deal with.It will then work work work corect work Wereness.efind.
Please note:if you use the code of my olderブログpost「Cusomize the contextual menu of UnibView」in your project、you need to udate this as well、because it also the element Frome Point
Posted in iPhone&iPod touch,Prograamming,Tips&tricks,Web Technology.
Tagged with Development、iOS、JavaScript、UICbView、WebKit.
12 comments
By Alexander–Octover 17,2011