繁体   English   中英

如何使此链接在javascript中起作用

[英]How do I make this link work in javascript

好的,基本上我有这个javascript文件http://assets.revback.com/scripts/share1.js ,该文件基本上通过javascript添加了一堆共享按钮。

我想做的是更改twitter图片链接以使用url缩短器:

所以代替:

<a href=\"http:\/\/twitter.com\/home?status=Interesting Post:(UURRLL)\" title=\"Click to share this page on Twitter\"><img src=\"http:\/\/assets.revback.com\/scripts\/images\/twitter.png\" border=\"0\"\/><\/a>

我想用

<a href="#" onClick="window.location='http://ko.ly?action=shorten&uri=' + window.location + '&dest=twitter.com/?status=Reading ';"><img src=http://assets.revback.com/scripts/images/twitter.png"><\/a>

但我需要使用JavaScript友好的语法编写最底层的代码。 即像上一个一样,而不是http://,您拥有http://

失去onclick 它没有任何好处,因为它的行为就像一个普通的链接(但更多断开的除外)。 现在,您不必担心在JavaScript中转义JavaScript以及随之而来的\\\\\\\\\\\\\\\\疯狂。

var buttonhtml= (
    '<a href="http://ko.ly?action=shorten&amp;uri='+encodeURIComponent(location.href)+'&amp;dest=twitter.com/?status=Reading">'+
        '<img src=http://assets.revback.com/scripts/images/twitter.png">'+
    '</a>'
);

(请注意, encodeURIComponent是将当前URL正确插入另一个URL而不中断的必不可少的encodeURIComponent ,它还可以防止HTML注入,因为<&字符已被%编码。在没有这种保护措施的情况下,任何包含脚本的页面具有跨站点脚本漏洞。)

更好的是,完全放弃HTML字符串后缀,并使用DOM方法创建内容。 然后,您无需担心&amp; 和其他HTML转义字符,并且您不必通过粗暴,不可靠的字符串替换来破坏HTML。 您似乎正在使用jQuery,因此:

var link= $('<a>', {href:'http://ko.ly?action=shorten&uri='+encodeURIComponent(location.href)+'&dest=twitter.com/?status=Reading'});
link.append('<img>', {src: 'http://assets.revback.com/scripts/images/twitter.png'});
link.appendTo(mydiv);

ETA:我将用循环替换整个标记混乱,并将数据分解成一个查找。 即。 就像是:

(function() {
    var u= encodeURIComponent(location.href);
    var t= encodeURIComponent(document.title);
    var services= {
        Facebook: 'http://www.facebook.com/sharer.php?u='+u,
        Twitter: 'http://ko.ly?action=shorten&uri='+u+'&dest=twitter.com/?status=Reading',
        StumbleUpon: 'http://www.stumbleupon.com\/submit?url='+u+'&title='+t,
        // several more
    };

    var share= $('<div class="ea_share"><h4>Share this with others!</h4></div>');
    for (var s in services) {
        share.append($('<a>').attr('href', services[s]).attr('title', 'Click to share this on '+s).append(
            $('<img>').attr('src', 'http://assets.styleguidence.com/scripts/images/'+s.toLowerCase()+'.png')
        ));
    }
    $('#question .vt').append(share);
})();

尝试这个

<a href="#" onClick="window.location='http://site.com?action=shorten&uri='+ 
  window.location + '&dest=twitter.com/?status=Reading;'">tweet this</a>

onclick属性中更改链接的href

<a href="#" onclick="this.href='http://site.com?action=shorten&uri=' + window.location + '&dest=twitter.com/?status=Reading';">tweet this</a>

除非事件处理程序onclick收到返回值false否则默认操作 (转到href属性指定的页面)将始终执行。 因此,在没有发生false之前更改href会使它转到您想要的页面。

<a href =“#” onClick =“ window.location =' http ://site.com?action=shorten&uri='+ window.location.href +'&dest = twitter.com /?status =正在读取';返回false ;“>发布此信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM