使用window.open()方法在twitter上分享报价
问题描述:
我正在创建一个应用程序,该应用程序通过单击按钮随机生成报价。我希望用户能够在Twitter或Facebook上分享这个报价,但这是问题出现的地方。使用window.open()方法在twitter上分享报价
HTML:
<div class="row">
<div id="quoteBox" class="col-xs-6 col-xs-offset-3">
<p><span id="famousQuote">"Nothing in life is to be feared; it is only to be understood. Now is the time to understand more so that we may fear less."</span></p>
<i id="fbicon" class="fa fa-facebook-official fa-3x" aria-hidden="true" style="color:white"></i>
<i id="twitterShare" class="fa fa-twitter fa-3x" aria-hidden="true" style="color:white"></i>
</div>
<div class="row">
<div class="col-xs-4 col-xs-offset-4">
<button id="generateQuote" type="button" class="btn btn-primary btn-block">Change Quote</button>
</div>
</div>
</div>
JAVASCRIPT: “对不起,该网页不存在”
document.getElementById("twitterShare").addEventListener("click", function(){
var url ="https://twitter.com/intent.tweet";
var text = document.getElementById("famousQuote").value;
window.open(url+"?text="+text);
})
然而,当我做点击Twitter的图标,链接显示说
很明显,我在这里得到的错误之一是URL,或者我写的方式。
非常感谢您的帮助。
答
您需要编码网址参数,并使用不同的URL:
document.getElementById("twitterShare").addEventListener("click", function(){
var url ="https://twitter.com/share?url=" + encodeURIComponent(document.location);
var text = encodeURIComponent(document.getElementById("famousQuote").innerText);
window.open(url + "&text=" + text);
})
'value'仅适用于表单控件的元素,而不是内容元素 – charlietfl