Car*_*osa 1 javascript redirect url-rewriting
如何使用单个onmousedown事件重写所有外部链接?
有点像facebooks用它的链接垫片做什么
请查看关于链接垫片的原始Facebook文章:http: //on.fb.me/zYAq0N
要求:
应该适用于mainsite.com上的所有子域.
应该将example.com重定向到mainsite.com/link.cgi?u=http://example.com
应该只重定向不属于mainsite.com的链接.
用途:
这是为了保护私人网络应用的用户安全,并保护推荐人.
如FB文章中所指出的,最好在动态上实现良好的UI.
当用户将鼠标悬停在某个链接上时,它会显示正确的链接,但是当他点击它时,js会将其重定向到my.site.com/link.cgi=http://link.com
提前致谢
对于个人链接:
function $(id){ return document.getElementById(id); }
$(link).onclick = function(){ this.href = "http://otherlink.com"; }
<a href="http://example.com" id="link">My Sheisty Link</a>
Run Code Online (Sandbox Code Playgroud)
所以要获得所有外部链接:
window.onload = function(){
var links = document.getElementsByTagName("a");
for(a in links){
var thisLink = links[a].href.split("/")[2];
if(thisLink !=== window.location.hostname){
links[a].href = "http://mainsite.com/link.cgi?u=" + links[a]; }
// Or you could set onclick like the example above
// May need to accommodate "www"
}}
Run Code Online (Sandbox Code Playgroud)
编辑:
找到这个答案并得到一个更好的主意.
document.onclick = function (e) {
e = e || window.event;
var element = e.target || e.srcElement;
if (element.tagName == 'A') {
var link = element.href.split("/")[2];
if(link !=== window.location.hostname){
links[a].href = "http://mainsite.com/link.cgi?u=" + links[a]; }
return false; // prevent default action and stop event propagation
}else{ return true; }
}};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2040 次 |
| 最近记录: |