简体   繁体   English

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

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

Ok basically I have this javascript file http://assets.revback.com/scripts/share1.js that basically adds a bunch of share buttons via javascript. 好的,基本上我有这个javascript文件http://assets.revback.com/scripts/share1.js ,该文件基本上通过javascript添加了一堆共享按钮。

What I want to do, is change the twitter image link to use an url shortener: 我想做的是更改twitter图片链接以使用url缩短器:

so instead of: 所以代替:

<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>

I want to use 我想用

<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>

but I need that bottom one, to be written with javascript friendly syntax. 但我需要使用JavaScript友好的语法编写最底层的代码。 ie like in the top one, instead of http://, you have http:// 即像上一个一样,而不是http://,您拥有http://

Lose the onclick . 失去onclick There is no benefit to it whatsoever, since it just acts like a normal link (except much more broken). 它没有任何好处,因为它的行为就像一个普通的链接(但更多断开的除外)。 Now you don't have to worry about escaping JavaScript inside JavaScript and the consequent \\\\\\\\\\\\\\\\ madness. 现在,您不必担心在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>'
);

(Note that the encodeURIComponent , which is essential to correctly inserting your current URL into another URL without breaking, is also protecting you from HTML-injection, since < and & characters get % -encoded. Without that safeguard, any page that includes your script has cross-site-scripting vulnerabilities.) (请注意, encodeURIComponent是将当前URL正确插入另一个URL而不中断的必不可少的encodeURIComponent ,它还可以防止HTML注入,因为<&字符已被%编码。在没有这种保护措施的情况下,任何包含脚本的页面具有跨站点脚本漏洞。)

Better still, lose the HTML string-slinging altogether and use DOM methods to create your content. 更好的是,完全放弃HTML字符串后缀,并使用DOM方法创建内容。 Then you don't need to worry about &amp; 然后,您无需担心&amp; and other HTML escapes, and you don't have to hack your HTML together with crude, unreliable string replacing. 和其他HTML转义字符,并且您不必通过粗暴,不可靠的字符串替换来破坏HTML。 You seem to be using jQuery, so: 您似乎正在使用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: I'd replace the whole markuppy mess with a loop and the data broken out into a lookup. ETA:我将用循环替换整个标记混乱,并将数据分解成一个查找。 ie. 即。 something like: 就像是:

(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);
})();

Try this 尝试这个

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

Change the href of the link in the onclick attribute: onclick属性中更改链接的href

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

The default action (going to the page designated by the href attribute) will always still be executed unless the event handler onclick receives a return value of false . 除非事件处理程序onclick收到返回值false否则默认操作 (转到href属性指定的页面)将始终执行。 So, changing the href before it happens will cause it to go to the page you want it to as long as you don't return false. 因此,在没有发生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