简体   繁体   English

从WebView字符串获取iframe src

[英]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.

相关问题 在didReceiveRemoteNotification中但在加载webView之后,使用stringByEvaluatingJavaScriptFromString - stringByEvaluatingJavaScriptFromString in didReceiveRemoteNotification but after webView is loaded 使用stringByEvaluatingJavaScriptFromString document.getElementsByClassName从UIWebView获取数据 - Get data from UIWebView with stringByEvaluatingJavaScriptFromString document.getElementsByClassName self.viewController.webView stringByEvaluatingJavaScriptFromString不起作用 - self.viewController.webView stringByEvaluatingJavaScriptFromString does not work UIWebView -stringByEvaluatingJavaScriptFromString:在iFrame内容中填充文本框? - UIWebView -stringByEvaluatingJavaScriptFromString: fill textbox in iFrame content? 以正确的方式加载html以使stringByEvaluatingJavaScriptFromString工作 - loading html the right way to get stringByEvaluatingJavaScriptFromString working 如何使用stringByEvaluatingJavaScriptFromString在Amazon上获取ASIN代码? - How to get ASIN code on Amazon using stringByEvaluatingJavaScriptFromString? 如何从目标C中的stringByEvaluatingJavaScriptFromString:方法执行长JavaScript? - How to execute long javascript from stringByEvaluatingJavaScriptFromString: method in objective C? 从Web视图中的链接获取参数 - Get parameters from a link in a webview 从 Webview 获取 JSON 值 - Get JSON values from Webview ios iframe Webview问题 - ios iframe issues with webview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM