0

I have a little problem when I passing url to javascript

this one is javascript file

function geturl(getdata) {

     document.getElementById('test001').src =   getdata;//Iframe don't display data.but if I put url like 'http://stackoverflow.com/questions/ask'  is will work. 

}
window.onload = function ()
{
    geturl();
}

and Obj-C

 NSString * getdata  = @"de";

[subView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"geturl('%@')",[getdata stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];

NSLog(@" getdata = %@",getdata);";

and Iframe

<iframe id="test001" src="" frameborder="0" hspace="0"  ></iframe>

How can I use url from Obj-c to javascript on Iframe. Thanks for any Idea.


Update

I try to display data inside getdata If I alert(getdata) is show correct data. but If I use this document.getElementById('test001').src = 'http://en.wikipedia.org/wiki/'+getdata; getdata will show undefined : http://en.wikipedia.org/wiki/undefined something like this. the problem is come from IOS or javascript advice me please.

Kabitis
  • 51
  • 7

1 Answers1

0

The output from [NSString stringWithFormat:@"geturl(%@)",[getdata stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];

will be

"geturl(http://someurl.com)"

when it needs to be

"geturl('http://someurl.com')"

Try changing your format string to

@"geturl('%@')"

mhw
  • 31
  • 1
  • Also you might want to try debugging it in a standard browser. If I use that url in chrome I see the error "Refused to display 'http://stackoverflow.com/questions/9826792/how-to-invoke-objective-c-method-from-javascript-and-send-back-data-to-javascrip' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'." Try using a different url (to a page that doesn't prevent itself being presented in a frame). – mhw Feb 18 '14 at 13:26