简体   繁体   English

将存储的值附加到Twitter意图URL

[英]Appending stored values to a twitter intent URL

I am running a name generator script that allows a user to input their first and last name, and then populates a div with their "silly name." 我正在运行一个名称生成器脚本,允许用户输入他们的名字和姓氏,然后使用“愚蠢的名字”填充div。 Here is that javascript: 这是javascript:

<script>

window.onload = initAll;

function initAll() {
    document.getElementById("submit-button").onclick = getSillyName;
}

function getSillyName() {
    document.getElementById("answer").innerHTML = getSillyNameString();
    return false;
}

function getSillyNameString() {
    var firstName = new Array("Peso", "Versace", "Purp", "Purple", "Palace", "Harlem", "Pretty", "Flacko", "Cake", "Goldie", "Trillmatic", "Lord", "Trill", "Ty", "Dolce", "Gucci", "Jodye", "Versace", "Guapanese", "Guapito", "Keef", "Reefer", "Codein", "Ford", "Papi", "Basquiat");
    var lastName1 = new Array("Swisher", "Benz", "Clams", "Kush", "Margiela", "Trillmatic", "Eleveny", "Berg", "Gordo", "1Train", "Phoenix", "Guapo", "1Train", "Dulce", "Laurent", "Milan", "Clams", "Uptown", "Lean", "Guapo", "Flackito", "Testarossa", "Swisha", "Pesos", "Beretta", "Balenciaga");

    var firstNm = document.getElementById("fName").value.toUpperCase();
    var lastNm = document.getElementById("lName").value.toUpperCase();
    var validName = true;

    if (firstNm == "") {
        validName = false;
    }
    else {
        var firstNum = firstNm.charCodeAt(0) - 65;
        if (firstNum < 0 || firstNum > 25) {
            validName = false;
        }
    }

    if (!validName) {
        document.getElementById("fName").focus();
        document.getElementById("fName").select();
        return "enter your name below";
    }

    if (lastNm == "") {
        validName = false;
    }
    else {
        var lastNum1 = lastNm.charCodeAt(0) - 65;

        if (lastNum1 < 0 || lastNum1 > 25) {
            validName = false;
        }
    }

    if (!validName) {
        document.getElementById("lName").focus();
        document.getElementById("lName").select();
        return "enter your name below";
    }

    return "Your name is " + firstName[firstNum] + " " + lastName1[lastNum1];
}

</script>

After they recieve their name, I would like to append the generated name to a twitter intent url: 收到他们的名字后,我想将生成的名字附加到Twitter意向网址:

<a id="custom-tweet-button" href="https://twitter.com/intent/tweet?text=My%20silly%20name%20is">Tweet</a>

How can I accomplish this URL append with the existing code? 如何使用现有代码完成此URL追加? Any help would be appreciated, and thanks! 任何帮助将不胜感激,谢谢!

I know this is pretty basic Javascript, but I figured it out. 我知道这是非常基本的Javascript,但我想通了。 I added to the last function, getSillyNameString(): 我添加到最后一个函数getSillyNameString():

var tweettext = firstName[firstNum] + "%20" + lastName1[lastNum1];
document.getElementById("custom-tweet-button").href = "https://twitter.com/intent/tweet?text=My%20A%24AP%20Mob%20name%20is%20" + tweettext +"&hashtags=A%24AP%20Mob%2CA%24AP%20Rocky&via=MTVNews&url=http%3A%2F%2Fon.mtv.com%2F17B95Hu";

return "Your name is A$AP " + firstName[firstNum] + " " + lastName1[lastNum1];

Twitter web intent needs the URL encoded, which I did manually. Twitter网络意图需要编码的URL,我手动完成。

This changes a placeholder href attribute dynamically. 这会动态更改占位符href属性。 Good to go! 很高兴去!

If anyone sees a better/ cleaner way to accomplish this, I'd be happy to hear it. 如果有人看到更好/更清洁的方式来实现这一目标,我会很高兴听到它。

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

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