0

After some trying I got the function which return me the html I select in iframe, the problem is it returns me the href attributes with local path, How can I get the full url path/absolute path before I return it out from the iframe? here is the function:

function getSelectionHtml(iframe) {
    var html = "";
    var win = iframe.contentWindow;
    var doc = win.document;
    if (typeof win.getSelection != "undefined") {
        var sel = win.getSelection();
        if (sel.rangeCount) {
            var container = doc.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof doc.selection != "undefined") {
        if (doc.selection.type == "Text") {
            html = doc.selection.createRange().htmlText;
        }
    }
    return html;
}

solved: actually I found a simple function that converts relative href to absolute from here

function qualifyURL(url) {
    var a = document.createElement('a');
    a.href = url;
    return a.href;
}

and added this to my code at the beginning which select all a tags and change them to absolute value:

var all_a = doc.getElementsByTagName("a");
for (var i = 0; i < all_a.length; i++) {
    all_a[i].href = qualifyURL(all_a[i].href);
};
Community
  • 1
  • 1
user1844923
  • 179
  • 1
  • 11

0 Answers0