简体   繁体   中英

Weird window.open behaviour

This code behaves funny on Chrome (fiddle here ):

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

First, despite the code being wrapped in a try-catch, an error is thrown:

Unable to open a window with invalid URL '%36:%04-='.

Second, extraneous characters are inserted in the URL, namely %3 and %04 .

Why doesn't the try-catch intercept the error, and why does the URL have those extra characters?

The try / catch doesn't have any effect because it's not an exception. 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.

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. 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.

First: As pimvdb said, it's because it isn't actually an exception.

Second: %04 is an invisible character inserted by JSFiddle. %36 is the number 6 which Chrome converts to %36 when encoding it for URL scheme. Updated fiddle without %04

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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