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

时间:2015-02-25 19:02:22

标签: javascript twitter dynamic

我正在运行一个名称生成器脚本,允许用户输入他们的名字和姓氏,然后用他们的"愚蠢的名字填充div。"这是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>

在收到他们的名字后,我想将生成的名字附加到Twitter意向网址:

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

如何使用现有代码完成此附加链接?任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

我知道这是非常基本的Javascript,但我想通了。我添加到最后一个函数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网络意图需要编码的URL,我手动完成。

这会动态更改占位符href属性。很高兴!

如果有人看到更好/更清洁的方式来实现这一点,我很高兴听到它。