Why are there non-breaking spaces in my page title?
I'm injecting a script via JSONP and using it to call a method in my web application like so:
(function jsonp(src){
var b = document.body;
var t = document.title;
var u = encodeURI(document.location.href);
var o = document.getElementById('srv-call');
o && b.removeChild(o);
var s = document.createElement('script');
s.id = 'srv-call';
s.src = src + '?w=' + textSelection + '&cb=autoCall&u=' + u + '&pt=' + t + '&t=' + (new Date().getTime());
b.appendChild(s);
})('http://localhost:8888/wordmark/words/add_word');
Unfortunately, my document.title is getting filled with non-breaking spaces. An example http request is this:
http://localhost:8888/wordmark/words/add_word?w=problems&cb=autoCall&u=http://www.boingboing.net/2010/10/01/kid-demonstrates-eng.html&pt=%E2%80%8BK%E2%80%8Bi%E2%80%8Bd%E2%80%8B%20%E2%80%8Bd%E2%80%8Be%E2%80%8Bm%E2%80%8Bo%E2%80%8Bn%E2%80%8Bs%E开发者_C百科2%80%8Bt%E2%80%8Br%E2%80%8Ba%E2%80%8Bt%E2%80%8Be%E2%80%8Bs%E2%80%8B%20%E2%80%8BE%E2%80%8Bn%E2%80%8Bg%E2%80%8Bl%E2%80%8Bi%E2%80%8Bs%E2%80%8Bh%E2%80%8B%20%E2%80%8Bl%E2%80%8Ba%E2%80%8Bn%E2%80%8Bg%E2%80%8Bu%E2%80%8Ba%E2%80%8Bg%E2%80%8Be%E2%80%8B%20%E2%80%8Bi%E2%80%8Bn%E2%80%8B%20%E2%80%8B2%E2%80%8B4%E2%80%8B%20%E2%80%8Ba%E2%80%8Bc%E2%80%8Bc%E2%80%8Be%E2%80%8Bn%E2%80%8Bt%E2%80%8Bs%E2%80%8B%20%E2%80%8B-%E2%80%8B%20%E2%80%8BB%E2%80%8Bo%E2%80%8Bi%E2%80%8Bn%E2%80%8Bg%E2%80%8B%20%E2%80%8BB%E2%80%8Bo%E2%80%8Bi%E2%80%8Bn%E2%80%8Bg&t=1285982312594
The script that is injected in the page has the correct src, but the HTTP request is incorrect. Any idea why these are being inserted and if I have any way to avoid this, other than parsing them out via regex?
Thanks so much for any help you can give.
And I just realized the culprit. I apologize for wasting everyone's time, but in the event anyone else runs across this problem, the issue was the SMRT Safari Extension to alter Safari's URL auto-complete feature. -1 for me for not disabling all extensions and trying multiple browsers. Thanks, all.
Have you tried with decodeURIComponent(t)
instead of just t
?
s.src = src + '?w=' + textSelection + '&cb=autoCall&u=' + u + '&pt=' + decodeURIComponent(t) + '&t=' + (new Date().getTime());
what you need to do is take the variable t off in your line that says
s.src = src + '?w=' + textSelection + '&cb=autoCall&u=' + u + '&pt=' + t + '&t=' + (new Date().getTime());
so your link would look something like this instead:
http://localhost:8888/wordmark/words/add_word?w=problems&cb=autoCall&u=http://www.boingboing.net/2010/10/01/kid-demonstrates-eng.html&&t=1285982312594
and if you must have the t variable then insert it into the line like so
....(code before) '&pt=' + decodeURIComponent(t) + (code after)......
Hope this helps. thanks
PK
Those are not non-breaking spaces, but zero-width spaces (U+200B). They are normally not visible, and may be present in the original title (for text wrapping, or whatever other reason).
精彩评论