I'm aware that there are plenty of methods to use SVG sprites in HTML. My preference to this date has been to use something like
<div class="container">
<svg class="icon">
<title>Icon Title</title>
<use xlink:href="/svg/sprite.svg#icon"/>
</svg>
</div>
However, now I wanted to load the sprite from a subdomain, like this:
<div class="container">
<svg class="icon">
<title>Icon Title</title>
<use xlink:href="https://sub.domain.com/svg/sprite-home.svg#icon"/>
</svg>
</div>
Unfortunately, this doesn't work as the file is not fetched. I've tried with <object>
as well, but that doesn't seem to work either.
So far, I'm only able to use
<?php include_once("https://sub.domain.com/svg/sprite.svg"); ?>
It's ok as a quick fix, as this doesn't involve much refactoring. However, this also means the HTML gets bloated.
With the <use>
method the sprite gets cached. But with the include
method the sprite isn't cached, only gets embedded, and so it is far from ideal.
Does anybody use any alternative to the php include method that is compatible with cross domain requests and browser caching?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…