我可以将私有变量传递给javascript中的另一个函数

时间:2017-08-10 10:24:47

标签: javascript html

我希望人们可以根据人们输入的名称与独特的链接分享Twitter。

首先,我创建了一个函数genurl,它可以根据人们输入名字生成url。

其次,我希望人们可以在Twitter上分享网址。

现在的问题是,每个人都有一个独特的网址。如何在shareTwitter函数中将url ($("#result").html(url))传递给变量attr1



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<title>Generate Your Referral URL Link</title>


<style>
h2 {
    margin: 5px 0;
    padding:0;
}
td:first-child {
	width:150px;
}
td {
	width:300px;
}
input {
	width:100%;
}
</style>

</head>
<body>

<!-- Genenrate a unique url interface -->

<h1 style="margin:30px 0 0 50px;">Generate Your Referral URL Link</h1>
<table style="margin:0 50px;">
<tr><td>Backer Name:</td><td><input type="text" name="name" id="name" class="name" placeholder="Your Kickstarter Display Name (Case Sensitive)"></td></tr>
<tr><td>Join Time:</td><td><input type="text" name="time" id="time" class="time" placeholder="Month/Year You Joined Kickstarter"></td></tr>
<tr><td>Reward You Pledged:</td>
	<td>
<select name="reward" class="reward">
		<option value=""></option>
		<option value="seb1">SUPER Early Bird: Charcoal/Snow</option>
		<option value="seb2">SUPER Early Bird: Oak</option>
		<option value="eb1">Early Bird: Charcoal/Snow</option>
		<option value="seb3">SUPER Early Bird: Onyx</option>
		<option value="seboo">Special Early Bird: Oak/Onyx</option>
		<option value="ks1">Kickstarter Special: Charcoal/Snow</option>
		<option value="seb4">Special Early Bird CONTINUED: Oak/Onyx</option>
		<option value="eb2">Early Bird: Oak</option>
		<option value="eb3">Early Bird: Onyx</option>
		<option value="ks2">Kickstarter Special: Oak</option>
		<option value="dr">Developer Reward</option>
		<option value="ks3">Kickstarter Special: Onyx</option>
		<option value="pc1">Personal Collection</option>
		<option value="pc2">Premium Collection</option>
		<option value="a4">Any 4</option>
		<option value="a5">Any 5</option>
</select></td></tr>
<tr><td></td></tr>
</table>
<div style="margin:0 50px;">
<button type="button" id='genurl' onclick="genurl()">Create</button>
</div>
<div id="result" style="margin: 0 30px;"></div>
<div class="msg" style="margin: 0 30px;"></div>
</div>

<div style="margin-top: 60px;">


	
<!-- share twitter with the unique url interface-->

<a href="javascript:void(0)" id="twitterShare" onclick="shareTwitter()" target="_blank">
	<img src="https://daks2k3a4ib2z.cloudfront.net/58bad0d628e47e3e2911d6aa/58bad0d728e47e3e2911d711_fa-twitter_100_0_02779e_none.png" />
	<br /><p>Share on Twitter</p>
</a>
</div>
 <script type="text/javascript">

//Generate unique url 
function genurl(){
var name = $(".name").val();
	
	name = name.trim();
	var time = $(".time").val();
	time = time.trim();
	var reward = $(".reward").val();

 	if(name.length > 0 && time.length > 0 && reward.length > 0) {

		var url = "https://www.kickstarter.com/projects/mobvoi/ticwatch-2-the-most-interactive-smartwatch?utm_campaign=kickstarter_backer&utm_medium=referral&utm_source=social_share&backer_id=";
		url = url + encodeURIComponent(name + "_" + time + "_" + reward);
		$("#result").html(url);	

	} else {
		$(".msg").html('<h2>please write Backer Name, Join Time and Reward You Pledged!</h2>');
	}

};


//share twitter with the unique url

function shareTwitter() {
	
	// How could i pass the $("#result").html(url) to variable attr1?
	var attr1 = genurl();


	var attr = "https://twitter.com/home?status=" + attr1;
    document.getElementById("twitterShare").setAttribute('href', attr);

}

 </script>

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

genurl 返回值。

function genurl(){

    var name = $(".name").val();

    name = name.trim();
    var time = $(".time").val();
    time = time.trim();
    var reward = $(".reward").val();

    if(name.length > 0 && time.length > 0 && reward.length > 0)
    {
        var url = "https://www.kickstarter.com/projects/mobvoi/ticwatch-2-the-most-interactive-smartwatch?utm_campaign=kickstarter_backer&utm_medium=referral&utm_source=social_share&backer_id=";
        url = url + encodeURIComponent(name + "_" + time + "_" + reward);
        return url;
    }
    else{
        $(".msg").html('<h2>please write Backer Name, Join Time and Reward You Pledged!</h2>');
        return null;
    }
}   

然后在shareTwitter函数中使用此值:

function shareTwitter() {

    // How could i pass the $("#result").html(url) to variable attr1?
    var attr1 = genurl();    
    if(attr1){    // check value was returned!
        var attr = "https://twitter.com/home?status=" + attr1;
        document.getElementById("twitterShare").setAttribute('href', attr);
    }

}