javascript - Do single/double quotes make a difference if nothing is escaped? -
as see it, both 'mars%22%3a%22'
, "mars%22%3a%22"
equivalent, nothing being escaped.
i have been creating javscript bookmarklet time now. @ 1 point, stopped working when pasted as-is bookmark in chrome.
i discovered solution after guess-and-check:
a pair of double quotes needed single quotes. why?
the following line single-quotes inside split() causes no problems in bookmarklet:
loaddoc("/page1/" + aarray[i].href.split('mars%22%3a%22')[1].slice(0,7),i);
the line below double quotes cause bookmarklet not run @ all:
loaddoc("/page1/" + aarray[i].href.split("mars%22%3a%22")[1].slice(0,7),i);
no error shown in console.
note double-quote version run fine if pasted javascript console directly!
what not understanding?
javascript not distinguish between single , double quotes in way language ruby (where string interpolation , backslash escape sequences work in double-quoted string). both types of quote have same meaning in javascript.
the 1 difference whichever type of quote use, same quote can't used inside string unless escape backslash or url-encode it.
in case of bookmarklet, browser unescapes entire javascript:...
url string before executing it. can test simple case javascript:alert('foo%22bar')
alert text foo"bar
.
so string mars%22%3a%22
converted mars":"
before code runs. since string has double-quotes in it, can used inside single-quoted string.
btw, hunch correct may attempting parse or manipulate json text here? if are, better use json.parse()
instead of raw string manipulation.
Comments
Post a Comment