从相对 URL 获取绝对 URL.(IE6 问题) [英] Getting an absolute URL from a relative one. (IE6 issue)

查看:26
本文介绍了从相对 URL 获取绝对 URL.(IE6 问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下函数将相对 URL转换"为绝对 URL:

I'm currently using the following function to 'convert' a relative URL to an absolute one:

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

这在大多数浏览器中运行良好,但 IE6 仍然坚持返回相对 URL!如果我使用 getAttribute('href'),它也会这样做.

This works quite well in most browsers but IE6 insists on returning the relative URL still! It does the same if I use getAttribute('href').

我能够从 IE6 中获取合格 URL 的唯一方法是创建一个 img 元素并查询它的 'src' 属性 - 问题在于它生成了一个服务器请求;我想避免的事情.

The only way I've been able to get a qualified URL out of IE6 is to create an img element and query it's 'src' attribute - the problem with this is that it generates a server request; something I want to avoid.

所以我的问题是:有没有办法从相对的(没有服务器请求)获取 IE6 中的完全限定 URL?

So my question is: Is there any way to get a fully qualified URL in IE6 from a relative one (without a server request)?

在你推荐一个快速的正则表达式/字符串修复之前,我向你保证它没有那么简单.基本元素 + 双周期相对 url + 大量其他潜在变量真的很糟糕!

Before you recommend a quick regex/string fix I assure you it's not that simple. Base elements + double period relative urls + a tonne of other potential variables really make it hell!

必须有一种方法可以做到这一点,而不必创建庞大的正则表达式解决方案??

There must be a way to do it without having to create a mammoth of a regex'y solution??

推荐答案

好奇怪!但是,当您使用 innerHTML 而不是 DOM 方法时,IE 会理解它.

How strange! IE does, however, understand it when you use innerHTML instead of DOM methods.

function escapeHTML(s) {
    return s.split('&').join('&amp;').split('<').join('&lt;').split('"').join('&quot;');
}
function qualifyURL(url) {
    var el= document.createElement('div');
    el.innerHTML= '<a href="'+escapeHTML(url)+'">x</a>';
    return el.firstChild.href;
}

有点难看,但比自己动手更简洁.

A bit ugly, but more concise than Doing It Yourself.

这篇关于从相对 URL 获取绝对 URL.(IE6 问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆