Microsoft Edge bug, empty href and base tag

1.1k Views Asked by At

Microsoft's new browser Edge doesn't seem to accept empty hrefs like <a href="">. In all other browsers, this can be used to link to the base tag. Though in Edge this doesn't seem to be the case.

I found a solution to this problem by setting the href to ./ in this question.

But having to change this on many sites is quite a hassle. Are there any other alternatives that do not require to change all the href's themselves?

Thanks in advance.

1

There are 1 best solutions below

0
On

Use a utility function to rewrite the empty hrefs. For example:

function rewrite_empty_hrefs()
  {
  var dom_parser = new DOMParser();

  var xml_serializer = new XMLSerializer();
  
  var old_html_dom = dom_parser.parseFromString(document.body.innerHTML,"text/html");

  var base_href = old_html_dom.querySelector("base").href;

  var old_xhtml_string = xml_serializer.serializeToString(old_html_dom);

  var new_xhtml_string = old_xhtml_string.replace(/(href=")(")/g, "$1"+base_href+"$2"); 

  document.body.innerHTML = new_xhtml_string;
  }

rewrite_empty_hrefs();
<base href="http://www.example.com">
<a href="">example.com</a>
<a href="">example.org</a>
<a href="">example.net</a>

References