This will be broken as soon as you use asset packages (you cannot get an absolute url based on the package) and your code does not play well with packages urls which are already absolute because of the base url.