使用 JavaScript 将文本添加到 url 的开头
Adding text to the beginning of url with JavaScript
我正在尝试制作一个小书签。它应该测试 url 是否包含某个单词,如果没有,它将对其进行编码并在 url 的开头添加一些文本。到目前为止,我已经想出了如何测试这个词。我为此使用的代码如下:
if (document.location.href.indexOf('math') === -1){
alert("Math detected");
}
我想像这样编码 url:如果它检测到的 url 是这样的,http://www.coolmath-games.com/
它应该重定向到 tunneler.pw/index.php?q=http%3A%2F%2Fwww.coolmath-games.com%2F
。理想情况下,它不会使用正则表达式,但如果使用也没什么大不了的。
如果它没有检测到该词,则不应执行任何操作。
编辑:如果有人好奇,这里是转换为书签形式的代码。
javascript:void%20function(){-1===document.location.href.indexOf(%22math%22)%26%26alert(%22Math%20detected%22)}();
试试这个:
if (document.location.href.indexOf('blocked') === -1){
document.location.href = "http://tunneler.pw/index.php?q=" + encodeURIComponent(document.location.href);
}
encodeUriComponent
将转义 URL,因此您可以在另一个 URL.
的查询字符串中使用它
我正在尝试制作一个小书签。它应该测试 url 是否包含某个单词,如果没有,它将对其进行编码并在 url 的开头添加一些文本。到目前为止,我已经想出了如何测试这个词。我为此使用的代码如下:
if (document.location.href.indexOf('math') === -1){
alert("Math detected");
}
我想像这样编码 url:如果它检测到的 url 是这样的,http://www.coolmath-games.com/
它应该重定向到 tunneler.pw/index.php?q=http%3A%2F%2Fwww.coolmath-games.com%2F
。理想情况下,它不会使用正则表达式,但如果使用也没什么大不了的。
如果它没有检测到该词,则不应执行任何操作。
编辑:如果有人好奇,这里是转换为书签形式的代码。
javascript:void%20function(){-1===document.location.href.indexOf(%22math%22)%26%26alert(%22Math%20detected%22)}();
试试这个:
if (document.location.href.indexOf('blocked') === -1){
document.location.href = "http://tunneler.pw/index.php?q=" + encodeURIComponent(document.location.href);
}
encodeUriComponent
将转义 URL,因此您可以在另一个 URL.