Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
Im cramming a whole javascript-function into a stringByEvaluatingJavaScriptFromString-function, and it works fine like this:

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]];
 
%d is not a valid specified for a NSString object. It tell stringWithFormat: that that argument will be a signed integer. You need to use %@ to tell it that you are passing a string.

This is all covered in the documentation.
 
That didnt work either. Must be some conflict of datatypes or something because of the long string.

I made a work-around that works fine. My string-management is not the finest, though. Is it possible to merge 3 strings at once, or do you have to do it as tediously as Ive done?

Code:
	NSString *letter = @"myId2";
	NSString *jsStringPart1 = @"var theElement=document.getElementById('";
	NSString *jsStringPart2 = @"');var selectedPosX=0;var selectedPosY=0;while(theElement!=null){selectedPosX+=theElement.offsetLeft;selectedPosY+=theElement.offsetTop;theElement=theElement.offsetParent;}window.scrollTo(selectedPosX,selectedPosY);";
	NSString *jsStringTemp1 = [jsStringPart1 stringByAppendingString:letter];
	NSString *jsString = [jsStringTemp1 stringByAppendingString:jsStringPart2];	
	
	[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:jsString]];
 
There is no way to append 3 strings, but you don't need your stringWithFormat: on the last line: jsString on it's own should work
 
Another problem with stringByEvaluatingJavaScriptFromString

Ive got a js-function in a webview that focuses on a select-list and selects it, which works fine.

Code:
function selectLetter()
{
document.getElementById("select_letter").focus();
document.getElementById("select_letter").select();
}

However, when invoking the same functionality from obj-c code using stringByEvaluatingJavaScriptFromString, it wont execute. Nothing happens.

Code:
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('select_letter').focus();"]];
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('select_letter').select();"]];

Ive tried other types of code with stringByEvaluatingJavaScriptFromString manipulating just that select-list, which has worked fine. For instance:

Code:
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('select_letter').style.visibility='hidden';"]];

Any suggestions?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.