简体   繁体   English

来自javascript值的HTML标记属性值

[英]Html tag attribute value from javascript value

im going crazy , im not so good on js or html so plz try to help me. 我快疯了,我对js或html不太好,所以请尝试帮助我。 on my blog on blogger i want to 1) Automatically create a short link using goo.gl service 2) share this short link on twitter by its button the js code im using to make the link short 在我想在博客上发布的博客上,我想1)使用goo.gl服务自动创建一个短链接2)通过其按钮js代码im使该链接简短来在Twitter上共享此短链接

<script type="text/javascript">
    var shorturl;
      var longUrl="http://*****" + "<data:blog.url/>";
      var request = gapi.client.urlshortener.url.insert({
      'resource': {
      'longUrl': longUrl
         }
       });
       request.execute(function(response) 
       {

        if(response.id != null)
        {
            shorturl=response.id;
        }
        else
        {
            shorturl="<data:blog.url/>";
            alert("error: creating short url n"+ response.error);
        }
        document.getElementById('shorturlid').setAttribute("data-url",shorturl);
    });
    </script>

the code for twitter button Twitter按钮的代码

<a href="https://twitter.com/share" class="twitter-share-button" id="shorturlid" data-via="*****" data-size="large" data-count="none" data-hashtags="*****">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

this just share the current page url so it seems that my code does nothing. 这只是共享当前页面的网址,所以看来我的代码什么也没做。

Any Solution? 有什么办法吗?

I've created a jsfiddle and there I have noticed that you can't access the link with shorturlid once twitter script have executed. 我已经创建了一个jsfiddle,并且我注意到,一旦执行了twitter脚本,就无法使用shorturlid访问链接。

Because it changes the DOM a lot. 因为它极大地改变了DOM。 A better way to do what you want is to call the twitter script after you have your shortened url ready and added to the DOM. 一种更好的方法是在准备好缩短的URL并将其添加到DOM后,调用twitter脚本。

My script seems to work but sometimes I'm getting the following error Error. User Rate Limit Exceeded. 我的脚本似乎可以正常工作,但有时会出现以下错误Error. User Rate Limit Exceeded. Error. User Rate Limit Exceeded. not sure what it is. 不知道是什么。 Maybe if you run the script too often. 也许如果您经常运行脚本。

(I have used jQuery beause it's easier to create DOM elements. Of course the same should work with pure JS.) (我之所以使用jQuery,是因为创建DOM元素更容易。当然,纯JS也应如此。)

var longurl = 'http://www.google.com/';
var $tw_link;

gapi.client.load('urlshortener', 'v1', function () {
    var request = gapi.client.urlshortener.url.insert({
        'resource': {
            'longUrl': longurl
        }
    });
    var resp = request.execute(function (resp) {
        if (resp.error) {
            $("#show").html('Error. ' + resp.error.message);
        } else {
            $("#show").html("Short URL for " + longurl + " is: " + resp.id);
            var shorturl = resp.id; //document.getElementById('shorturlid').setAttribute("data-url", shorturl); // not working because twitter button changes DOM!!
            $tw_link = $('<a/>');
            $tw_link.attr('href', 'http://twitter.com/share');
            $tw_link.addClass('twitter-share-button');
            $tw_link.attr('data-url', shorturl);
            //var body = document.getElementsByTagName('body');
            $('#show').append($tw_link);
            console.log(shorturl, $tw_link);
            loadTwitter();
            //alert(shorturl);
        }
    });
});

function loadTwitter() {
    ! function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0],
            p = /^http:/.test(d.location) ? 'http' : 'https';
        if (!d.getElementById(id)) {
            js = d.createElement(s);
            js.id = id;
            js.src = p + '://platform.twitter.com/widgets.js';
            fjs.parentNode.insertBefore(js, fjs);
        }
    }(document, 'script', 'twitter-wjs');
};

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

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