【发布时间】:2011-11-07 10:23:14
【问题描述】:
我正在尝试创建一个 Javascript 小书签,它将从 URL 中提取数字 ID:
http://site-1/script-1.php?ID=12345678&other-params
然后将浏览器重定向到一个新的 URL:
http://site-2/script-2.php?id=12345678
我已经在网上搜索了书签示例,我有 2 个问题:
1) 我需要将我的 JS 代码放在 void 中吗?
javascript:void(XXX);
void 在这种情况下是什么意思?
2) 我应该使用 location.href 来读取旧 URL 并分配一个新构建的 URL 吗?我之所以问,是因为我见过许多奇怪的代码,例如 String(location.href) 等 - 就像它需要对 String 进行一些 casting (是吗? )
更新:我尝试了以下代码,但不幸的是,当我在旧页面上并单击我的书签(希望导航到新页面)时,Chrome 中没有任何反应:
javascript:void (
if ( var match = location.search.match(/id=(\d+)/i) )
location = 'http://site-2/script-2.php?id=' + match[1];
)
(我在上面添加了换行符,是的,我正在尝试使用单个“=”)
更新 2:我的第二次尝试 - 只是尝试显示一个新 URL:
javascript:void (
var match = location.search.match(/ID=(\d+)/);
alert('http://site-2/script-2.php?id=' + match[1]);
)
但当我单击书签栏上的书签时,仍然没有任何反应。我在 Google Chrome 控制台中看到:
Uncaught SyntaxError: Unexpected token var
【问题讨论】: