简体   繁体   English

奇怪的window.open行为

[英]Weird window.open behaviour

This code behaves funny on Chrome (fiddle here ): 此代码在Chrome上表现得很有趣( 在这里小提琴):

try {
   open('6:-=');
} catch(e) {}

First, despite the code being wrapped in a try-catch, an error is thrown: 首先,尽管代码被包装在try-catch中,但仍会抛出错误:

Unable to open a window with invalid URL '%36:%04-='. 无法打开包含无效网址'%36:%04- ='的窗口。

Second, extraneous characters are inserted in the URL, namely %3 and %04 . 其次,在URL中插入无关字符,即%3%04

Why doesn't the try-catch intercept the error, and why does the URL have those extra characters? 为什么try-catch没有拦截错误,为什么URL有这些额外的字符?

The try / catch doesn't have any effect because it's not an exception. try / catch没有任何效果,因为它不是例外。 It's merely an error message printed to the console. 它只是打印到控制台的错误消息 You can proof that: 你可以证明:

open('6:-=');
console.log(1);  // logged as usual

Basically, it's just like console.error() doesn't throw an exception either, yet it prints an exception-like message to the console. 基本上,它就像console.error()也没有抛出异常,但它会向控制台输出类似异常的消息。

Your fiddle contains a non-printable character with ASCII code 4 in the 6:-= string after the colon, which is URL-encoded as %04 in the displayed error. 您的小提琴包含一个不可打印的字符,其中包含6:-= ASCII码4 6:-=冒号后面的字符串,在显示的错误中以URL编码为%04 In addition, the 6: part of the provided URL is interpreted as an URL scheme, which cannot start with a digit, so apparently Chrome URL-quotes the 6 as %36 as well, although such behavior is not prescribed by the RFC. 此外,提供的URL的6:部分被解释为URL方案,不能以数字开头,因此显然Chrome网址引用6也为%36 ,尽管RFC没有规定这种行为。

First: As pimvdb said, it's because it isn't actually an exception. 第一:正如pimvdb所说,这是因为它实际上并不是例外。

Second: %04 is an invisible character inserted by JSFiddle. 第二:%04是由JSFiddle插入的不可见字符。 %36 is the number 6 which Chrome converts to %36 when encoding it for URL scheme. %36是Chrome为URL方案编码时转换为%36的数字6。 Updated fiddle without %04 更新了没有%04的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM