Randomblue
Randomblue

Reputation: 116383

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?

Upvotes: 2

Views: 650

Answers (3)

Some Guy
Some Guy

Reputation: 16210

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

Upvotes: 2

lanzz
lanzz

Reputation: 43198

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.

Upvotes: 3

pimvdb
pimvdb

Reputation: 154928

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.

Upvotes: 5

Related Questions