// Takes a url, a GET parameter name and value and returns
// the given URL but with the given parameter at the end of
// the query portion.
function urlWithParameter(url, name, value) {
// Find the fragment since the query ends where the fragment starts.
var fragmentStart = url.indexOf('#');
if (fragmentStart < 0) { fragmentStart = url.length; }
var urlBeforeFragment = url.substring(0, fragmentStart);
// If there is no query (no '?' in URL) then start the parameter with
// a '?' to create a query. Otherwise separate the parameter from
// the existing query with a '&'.
// We use encodeURIComponent which assumes UTF-8 to escapes special URL
// characters like '#', '&', '?', '%', and '='.
// It assumes UTF-8. Any replacement that does not assume UTF-8 must escape
// at least the code-points listed above.
return urlBeforeFragment + (urlBeforeFragment.indexOf('?') < 0 ? '?' : '&')
+ encodeURIComponent(name) + '=' + encodeURIComponent(value)
+ url.substring(fragmentStart);
}
可因此用于
<script>/* the function above */</script>
<a onclick="this.href = urlWithParameter('http://twitter.com/', 'status', tab.url)" href="#">...</a>
做你想做的事,同时仍然尊重链接target
隐含<base target="...">
,仍然显示悬停的有用的网址。
或者,如果你只需要操作一个参数,那么您可以使用您尽早解决这样
<a onclick="this.href = 'http://twitter.com/?status=' + encodeURIComponent(tab.url)" href="http://twitter.com/">...</a>
编辑:为了得到它与异步铬存取工作,请尝试以下操作:
<script>
function redirectWithSelectedTabUrl(link) {
chrome.tabs.getSelected(null, function (tab) {
window.location.href = tab.url
? link.href + "?status=" + encodeURIComponent(tab.url)
: link.href;
};
return false;
}
</script>
<a href="http://twitter.com/" onclick="return redirectWithSelectedTabUrl(this)">...</a>
这很简单,适用于各种浏览器,但它会忽略target
,并且可能不会沿引用标头发送。
然后呢?问题似乎不完整。发生了什么错误,发生了什么,预期的是什么? – 2011-05-08 18:13:07
它加载没有URL的网页,它应该打开带有链接的URL。 https://twitter.com?status=example.com然而它加载“状态?=”(没有链接) – itsdaniel0 2011-05-08 18:15:20