Im cramming a whole javascript-function into a stringByEvaluatingJavaScriptFromString-function, and it works fine like this:
As you can see, the js-code looks for 'myId' in the html-code, and scrolls to that element.
However, I want to be able to swith 'myId' for other id's dynamically. But when trying to insert a string into where 'myId' is by using the following code, it stops working. Do you have any idea what I am doing wrong?
For clarity I'll post each line of the js-code wiht a new line. Apparently the code needs to be without linebreaks to work, though.
Code:
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var theElement=document.getElementById('myId');var selectedPosX=0;var selectedPosY=0;while(theElement!=null){selectedPosX+=theElement.offsetLeft;selectedPosY+=theElement.offsetTop;theElement=theElement.offsetParent;}window.scrollTo(selectedPosX,selectedPosY);"]];
As you can see, the js-code looks for 'myId' in the html-code, and scrolls to that element.
However, I want to be able to swith 'myId' for other id's dynamically. But when trying to insert a string into where 'myId' is by using the following code, it stops working. Do you have any idea what I am doing wrong?
Code:
NSString *theID = @"myId2";
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var theElement=document.getElementById(%d);var selectedPosX=0;var selectedPosY=0;while(theElement!=null){selectedPosX+=theElement.offsetLeft;selectedPosY+=theElement.offsetTop;theElement=theElement.offsetParent;}window.scrollTo(selectedPosX,selectedPosY);", theID]];
For clarity I'll post each line of the js-code wiht a new line. Apparently the code needs to be without linebreaks to work, though.
Code:
NSString *theID = @"myId2";
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var theElement=document.getElementById(%d);
var selectedPosX=0;
var selectedPosY=0;
while(theElement!=null)
{selectedPosX+=theElement.offsetLeft;
selectedPosY+=theElement.offsetTop;
theElement=theElement.offsetParent;}
window.scrollTo(selectedPosX,selectedPosY);",
theID]];