简体   繁体   中英

How can I use java,html or javascript to convert relative href attributes into absolute paths?

I would like to open an html file locally (not fetch it from a server) and somehow ensure that the relative hrefs are turned correctly resolved. I suppose I need to convert them into absolute paths.

I played around with the html element, in the head of the document, to resolve any relative links on the page, but only had limited success (often the resources could not be found).

I can convert the html source file anyway I wish with java, but ideally I would like to modify the html source as little as possible, for example insert a function that modifies the result of the hrf attribute.

How would I go about this? Do any of the java html parsers do this?

Thanks

If you want to do it on your own, I recommend you to read the chapter Reference Resolution in RFC 3986 . It explains how to establish the base URI and how to resolve relative URIs by giving an algorithm.

But there's probably already a library in Java that does that for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM