IE8 window.open 名称 - 不喜欢 JavaScript 编码?

发布于 2024-10-02 00:30:22 字数 406 浏览 0 评论 0原文

我这样调用 window.open() :

window.open('blank.html', 'New_Window\x3a_Jamie', 'width=800,height=800');

我在代码中所做的就是获取窗口的名称,然后 JavaScript 使用 Microsoft Web 保护库。我还用下划线替换空格,因为我读到 IE 不喜欢窗口名称中的空格。仅供参考,原始字符串是“New Window: Jamie”,看起来“:”被编码为“\x3a”。窗口在 FireFox 中打开正常,但在 IE8 中打不开。 IE8 不喜欢这种编码、字符还是什么? IE8 的窗口名称中可以出现哪些字符有规则吗?

I'm calling window.open() like this:

window.open('blank.html', 'New_Window\x3a_Jamie', 'width=800,height=800');

What I've done in the code is taken the window's name and JavaScript encoded it using the Microsoft Web Protection library. I'm also replacing spaces with underscores because I read that IE doesn't like spaces in window names. FYI the original string was "New Window: Jamie" and it looks like the ":" gets encoded as "\x3a". The window opens in FireFox just fine, but the window does not open in IE8. Does IE8 just not like this encoding, or the character or what? Are there rules around what characters can appear in the window name for IE8?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梨涡少年 2024-10-09 00:30:22

对于 IE8 的窗口名称中可以出现哪些字符有规则吗?

是的。尽管似乎没有文档记录,但 IE 始终要求窗口名称由字母数字和下划线组成。无论是否从编码的字符串文字中读取,都不会接受冒号。

如果您确实需要将任意字符串映射到唯一的名称安全版本,则必须执行一些操作,例如将每个非字母数字字符编码为转义序列,例如:

function encodeToName(s) {
    return s.replace(/[^A-Za-z0-9]/g, function(match) {
        var c= match[0].charCodeAt(0).toString(16);
        return '_'+(new Array(5-c.length).join('0'))+c;
    });
}

alert(encodeToName('New Window: Jamie'));
// 'New_0020Window_003A_0020Jamie'

我同意卡萨布兰卡,但实际上您似乎不太可能这样做需要这样做。用户永远不会看到窗口名称,因此 w1 也同样好。您需要窗口名称的情况非常罕见。

Are there rules around what characters can appear in the window name for IE8?

Yes. Although it doesn't seem to be documented, IE has always required that a window name be composed of alphanumerics and underscore. A colon won't be accepted, whether read from an encoded string literal or not.

If you really needed to map an arbitrary string to a unique name-safe version you'd have to do something like encoding every non-alphanumeric character into an escape sequence, eg:

function encodeToName(s) {
    return s.replace(/[^A-Za-z0-9]/g, function(match) {
        var c= match[0].charCodeAt(0).toString(16);
        return '_'+(new Array(5-c.length).join('0'))+c;
    });
}

alert(encodeToName('New Window: Jamie'));
// 'New_0020Window_003A_0020Jamie'

I agree with casablanca though, it seems very unlikely you should actually need to do this. The user is never going to get to see the window name, so w1 is just as good. It's rare enough that you need window names at all.

神仙妹妹 2024-10-09 00:30:22

我认为它希望窗口名称能够作为标识符。因此,“New_Window_Jamie”可能没问题。

I think it wants the window name to be something that'd work as an identifier. Thus, "New_Window_Jamie" would probably be OK.

耳钉梦 2024-10-09 00:30:22

您真的需要窗口名称吗?来自文档

当指定 元素或

的 target 属性时,此类字符串可用作链接和表单的目标。该字符串参数不应包含任何空格。

这是指定名称的唯一用途,虽然我没有看到除了“无空格”之外的任何限制,但只使用字母、数字和下划线是安全的。

Do you really need a window name? From the docs:

Such string can be used to be the target of links and forms when the target attribute of an <a> element or of a <form> is specified. This string parameter should not contain any blank space.

That's about the only use of specifying a name, and though I don't see any restrictions apart from "no spaces", it would be safe to just stick to letters, digits and underscores.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文