Is there a way to rewrite all relative URL:s when inserting a HTML fragment in jQuery?
I'm loading a HTML fragment into a page using a jQuery AJAX call. The fragment is loaded f开发者_运维问答rom a separate directory on the server and I want to be able to reuse it (with its links to scripts and images) in different locations.
Is there a safe way using JavaScript/jQuery to translate all relative URL:s in a page - relative to where the HTML fragment was loaded from - before inserting it into the DOM?
If you wanted to modify the href
attribute of every link, you could do...
$(document.links).attr('href', function(index, href) {
return window.location.pathname + '/' + href;
});
精彩评论