Skip to content

Commit 038bb32

Browse files
author
Tim Yates
committed
Create <a> instead of <img> when resolving URL properties
1 parent f9edde6 commit 038bb32

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

jquery.microdata.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,29 @@
9494

9595
function resolve(elm, attr) {
9696
// in order to handle <base> and attributes which aren't properly
97-
// reflected as URLs, insert a temporary <img> element just before
98-
// elm and resolve using its src attribute. the <img> element must
97+
// reflected as URLs, insert a temporary <a> element just before
98+
// elm and resolve using its href property. the <a> element must
9999
// be created using the parent document due IE security policy.
100100
var url = elm.getAttribute(attr);
101101
if (!url)
102102
return '';
103103
var a = ancestor(elm);
104104
var p = elm.parentNode;
105-
var img = (a.createElement ? a : document).createElement('img');
105+
var div = (a.createElement ? a : document).createElement('div');
106106
try {
107-
img.setAttribute('src', url);
107+
// Setting the href attribute/property on an <a> element
108+
// directly doesn't trigger resolution against the base URL in
109+
// IE, but creating an <a> element with innerHTML does. We have
110+
// to do some acrobatics to avoid having to HTML-escape the URL.
111+
// See http://stackoverflow.com/a/22918332/250798
112+
div.innerHTML = '<a></a>';
113+
div.firstChild.href = url;
114+
div.innerHTML = div.innerHTML;
108115
if (p)
109-
p.insertBefore(img, elm);
110-
url = img.src;
116+
p.insertBefore(div, elm);
117+
url = div.firstChild.href;
111118
if (p)
112-
p.removeChild(img);
119+
p.removeChild(div);
113120
} catch (e) {
114121
// IE>6 throws "TypeError: Access is denied." for mailto:
115122
// URLs. This is annoying, but harmless to ignore.

0 commit comments

Comments
 (0)