简体   繁体   English

Javascript书签:编码的URI

[英]Javascript Bookmarklet: Encoded URI

I have a piece of code which should open a website. 我有一段代码可以打开一个网站。

javascript:(function(){var google = function(){window.location='http://google.de?q=%3D%3D'};google()}())

When pasting the code to the console (Chrome) the website is opened and the URI is correctly encoded. 将代码粘贴到控制台(Chrome)时,将打开网站并正确编码URI。

Using the exact same code in a JS bookmarklet, it opens the website but with the decoded URI which causes my target page to fail. 使用JS小书签中的完全相同的代码,它将打开网站,但带有已解码的URI,这会导致我的目标页面失败。

Is there anything I can do to make the bookmarklet work? 我可以做些什么来使小书签起作用?

PS: In the example I use the already encoded URI, placing encodeURI or encodeURIComponent into the bookmarklet didn't help. PS:在示例中,我使用已经编码的URI,将encodeURIencodeURIComponent放入书签无济于事。

Special characters in a URL are decoded when the URL is parsed. 解析URL时,会对URL中的特殊字符进行解码。

A bookmarklet is a URL. 小书签是URL。

So when the bookmarklet is parsed, the special characters are decoded. 因此,当解析小书签时,特殊字符将被解码。

This happens before the URL inside the bookmarklet is parsed, so they are already decoded when it is parsed. 这是在解析小书签内的URL之前发生的,因此在解析小书签时已对它们进行了解码。

In short: You have a URL inside another URL. 简而言之:您在另一个URL中有一个URL。 You need to encode the special characters twice. 您需要对特殊字符进行两次编码。

javascript:(function()%7Bvar%20google%20%3D%20function()%7Bwindow.location%3D'http%3A%2F%2Fgoogle.de%3Fq%3D%253D%253D'%7D%3Bgoogle()%7D())

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

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