sarnabtech
sarnabtech

Reputation: 496

Get iframe src from webview stringByEvaluatingJavaScriptFromString

I have an URL being loaded into a webview, when the loading is complete, I want to get the iframe src that is inside the HTML, and reload the webview with the iframe src.

<iframe frameborder="0" allowtransparency="true" marginheight="0" marginwidth="0" scrolling="no" src="https://example.com/abc"></iframe>

In my iOS code, inside webViewDidFinishLoad , I am trying to get the iframe using

webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('iframe').attr('src')"];

This javascript is working in chrome console, however its not working for me.

Can you please help me?

Upvotes: 0

Views: 1064

Answers (1)

markwatsonatx
markwatsonatx

Reputation: 3491

I'm surprised document.getElementById works when you don't actually have an id attribute on the iframe. Try this instead:

document.getElementsByTagName('iframe')[0].src

Here is a JSFiddle that works in Safari:

https://jsfiddle.net/5vfv24wu/

Upvotes: 1

Related Questions