我如何获得这个“分享” 按钮来获取当前 URL 的 HTML 标题以及 URL?
我是一名 JavaScript 新手,正在尝试构建一个基本的共享按钮作为学习项目。 我对某一方面有点困惑。 虽然我可以获取当前页面的 URL 并将其传递到预填充共享表单的 URL,但我不确定如何获取 HTML 标题。
到目前为止,我要做的是:
<a href="http://www.example.com/submit" onclick="window.location = 'http://www.example.com/submit?url=' + encodeURIComponent(window.location); return false;"> Share This </a>
我应该在 onclick
部分添加什么才能获取当前页面的标题? 无论它是什么,都会在 URL 中作为 title=
传递。
奖励:是否可以添加一些内容来发送当前页面中的一些突出显示的文本? 这将作为 body=
出现在 URL 中,
所以我希望填补这些空白:
<a href="http://www.example.com/submit" onclick="window.location = 'http://www.example.com/submit?url=' + encodeURIComponent(window.location); return false; + 'title=' + SOMETHING + 'body=' + SOMETHING'"> Share This </a>
至少我是这么认为的。 我不能 100% 确定 + 和 ' 位于正确的位置。
I'm a JavaScript novice trying to build a basic share button as a learning project. I'm a bit stumped about one aspect. While I can grab the URL of the current page and pass that into the URL that prepopulates the share form, I'm not sure how to grab the HTML title.
Here's what I have to far:
<a href="http://www.example.com/submit" onclick="window.location = 'http://www.example.com/submit?url=' + encodeURIComponent(window.location); return false;"> Share This </a>
What do I add to the onclick
section to get the current page's title as well? Whatever it is would be passed as title=
in the URL.
Bonus: Is there something I can add to send along some highlighted text from the current page? That would go in the URL as body=
So I'm looking to fill in these blanks:
<a href="http://www.example.com/submit" onclick="window.location = 'http://www.example.com/submit?url=' + encodeURIComponent(window.location); return false; + 'title=' + SOMETHING + 'body=' + SOMETHING'"> Share This </a>
At least I think so. I'm not 100% sure I've got the +'s and ''s in the right place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你想要 document.title。 获取选定的文本有点复杂,特别是如果您想支持 IE。 DOM 标准是 window.getSelection 但对于 IE 你必须搞乱 < a href="http://msdn.microsoft.com/en-us/library/ms533041%28VS.85%29.aspx" rel="nofollow noreferrer">范围 - 我还没有检查是否有在 IE8 中有所改进。
我还想问一下,分享按钮在哪里? 如果它在页面中,则单击该按钮将取消选择所选的文本。
I think you want document.title. Getting selected text is a bit more complicated, especially if you want to support IE. DOM standard is window.getSelection but for IE you have to mess around with ranges - I've not checked if things have improved in IE8.
Also I meant to ask, where will the share button be? If it's in the page then clicking on the button is going to de-select the text which is selected.
获取所选文本的方法有多种,不同的浏览器实现不同的方法:
首先,有
document.getSelection()
,它以字符串形式返回所选文本。 然后,有window.getSelection()
,它将返回一个选择对象。 要获取原始文本,请使用其toString()
方法。 IE 获取文本的方式使用范围,即document.selection.createRange().text
。我建议使用包装函数来使
document.getSelection()
可用于支持上述任何方法的所有浏览器:作为旁注:您应该设置
location。 href
(而不是location
)来更改文档的地址。There are several ways to get the selected text, and different browsers implement different ones:
First, there's
document.getSelection()
, which returns the selected text as a string. Then, there'swindow.getSelection()
, which will return a selection object. To get the raw text, use itstoString()
method. The IE way of getting the text uses ranges, iedocument.selection.createRange().text
.I'd suggest use of a wrapper-function to make
document.getSelection()
available for all browsers which support any of the methods mentioned above:As a side note: You should set
location.href
(and notlocation
) to change the document's address.