[英]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. 我有一个URL加载到webview中,加载完成后,我想获取HTML内的iframe src,然后用iframe src重新加载webview。
<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 在我的iOS代码中,在webViewDidFinishLoad中,我试图使用
webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('iframe').attr('src')"]; webView stringByEvaluatingJavaScriptFromString:@“ document.getElementById('iframe')。attr('src')”];
This javascript is working in chrome console, however its not working for me. 这个javascript在chrome控制台中工作,但是对我来说不起作用。
Can you please help me? 你能帮我么?
I'm surprised document.getElementById
works when you don't actually have an id
attribute on the iframe. 我很惊讶,当您在iframe上实际上没有
id
属性时, document.getElementById
起作用。 Try this instead: 尝试以下方法:
document.getElementsByTagName('iframe')[0].src
Here is a JSFiddle that works in Safari: 这是可以在Safari中使用的JSFiddle:
https://jsfiddle.net/5vfv24wu/ https://jsfiddle.net/5vfv24wu/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.