Paths in Web Components are Relative to Root

前端 未结 3 1971
余生分开走
余生分开走 2020-12-29 09:16

I am creating a web component using native implementation, which in it\'s html template has links to images. However, those links only work if they are absolute, or relative

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 09:43

    This behavior seems to be specific to images.
    For script and link tags, relative paths from imported document work as expected.

    Also I noticed that this is not something specific to polyfills, even for native implementation(chrome) this issue(?) seems to be there.

    Seems like only option here is to include a script in your imported html, which will convert these relative paths to their absolute counterparts.
    To solve it elegantly you can avoid hardcoding urls in your script and generate it using url of your importing document. You can get that from document.currentScript.ownerDocument(or document._currentScript.ownerDocument in polyfilled scenario).

    So to answer your second question, I don't see any difference in native and polyfilled implementation at least in terms of behavior of src and href attributes.
    Quote you mentioned from http://webcomponents.org/polyfills/html-imports/ seems to be specific to href/src of script and link tags and they work as written.

    Hope it helps.

提交回复
热议问题