admin管理员组文章数量:1296278
I'm loading a string of HTML into a UIWebView through loadHTMLString. It produces a very long webpage. Once that string is loaded, I need to navigate to an HTML anchor tag with its "name" attribute set. In the HTML, I might have:
//3 pages of text here
<a name="go here"></a> lots more text here
//another 3 pages of text here
I need the webpage to scroll down to "go here" once loaded. The mappings work fine if a user clicks a link inside of the webpage and it loads an external URL. But in this case, I need to initially scroll down an already loaded webpage.
I can still execute JavaScript code like this:
[MyWebView loadHTMLString:dataString baseURL:[NSURL URLWithString:@";]];
[webView stringByEvaluatingJavaScriptFromString:@"javascriptFunc('param1');"];
But I need something similar for navigating to the anchor tag. Any suggestions?
I'm loading a string of HTML into a UIWebView through loadHTMLString. It produces a very long webpage. Once that string is loaded, I need to navigate to an HTML anchor tag with its "name" attribute set. In the HTML, I might have:
//3 pages of text here
<a name="go here"></a> lots more text here
//another 3 pages of text here
I need the webpage to scroll down to "go here" once loaded. The mappings work fine if a user clicks a link inside of the webpage and it loads an external URL. But in this case, I need to initially scroll down an already loaded webpage.
I can still execute JavaScript code like this:
[MyWebView loadHTMLString:dataString baseURL:[NSURL URLWithString:@"http://www.myscheme."]];
[webView stringByEvaluatingJavaScriptFromString:@"javascriptFunc('param1');"];
But I need something similar for navigating to the anchor tag. Any suggestions?
Share Improve this question edited Aug 10, 2021 at 0:48 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Apr 17, 2009 at 14:14 4thSpace4thSpace 44.4k101 gold badges306 silver badges493 bronze badges2 Answers
Reset to default 10You can jump to an anchor in an already-loaded HTML file by doing:
[webView stringByEvaluatingJavaScriptFromString:@"window.location.hash='#foo'"];
This will do the jump, sans animation.
If I'm not mistaken, just add #go%20here to the end of the URL.
Ahh, you mean already loaded. Is it really out of the question to just do reload the page? The browser should have cached most of it anyway...
本文标签: javascriptHow to reference an HTML anchorStack Overflow
版权声明:本文标题:javascript - How to reference an HTML anchor - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741635289a2389606.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论