通过< a href>向URL添加参数.刚刚点击 [英] Add parameters to an URL from a <a href> just clicked
问题描述
场景是很多html文件,它们之间还有更多链接.当我调用它们的第一个(将是索引)时,链接会通过URL传递几个参数(我们可以将它们称为首选项).
The scenario is a lot of html files with a lot more of links between them. When I call the first one of them (it would be the index), the link pass several parameters through the URL (we could call them preferences).
现在,我希望单击页面的几个链接中的任何一个时,都可以添加这些参数.因此,问题将与此类似(
Now I want that, when clicking any of the several links of the page, those parameters would be added. So the problem would be similar to this other ( How to add parameters to a URL that already contains other parameters and maybe an anchor) but doing it just after clicking the link.
我知道一种解决方案可以更改每个链接上的 onclick 事件,但是由于可能有成千上万个事件,没有常规的url格式,因此...正在寻找可以放在开头的脚本中的解决方案;可能与 onbeforeunload 事件有关.
I know that one solution could be changing the onclick event on each link, but as there may be thousands of them, without a regular url format... I'm looking for a solution that could be on the script at the head; perhaps something relative to onbeforeunload event.
无论如何我都找不到方法.有什么想法吗?
Anyway I couldn't find how to do it. Any ideas?
推荐答案
这将在被单击时将字符串附加到包含href属性的任何内容上.
This will append a string to anything containing an href-attribute when being clicked:
window.addEventListener("click", function(e) {
var href = e.target.getAttribute("href");
if(href) {
location.href = href + "?q=stackoverflow";
e.preventDefault();
}
});
此处的示例: http://jsfiddle.net/E5Q7P/
不适用于<IE9
Won't work in < IE9
这篇关于通过< a href>向URL添加参数.刚刚点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!