如何获取当前页面标题并将其作为JavaScript中的变量直接添加到链接中

如何获取当前页面标题并将其作为JavaScript中的变量直接添加到链接中,javascript,html,facebook,hyperlink,Javascript,Html,Facebook,Hyperlink,我正试图用html和javascript创建一个facebook共享代码 我希望链接完全像这样: <a href="http://www.facebook.com/sharer/sharer.php?u=http://lmn.9nty.com/?t=PAGETITLE+++-+learnmorenigeria+website%26l=www.learnmoreng.com/site_responcepage.html"> share </a> 但现在的问题是,如何仅

我正试图用html和javascript创建一个facebook共享代码 我希望链接完全像这样:

<a href="http://www.facebook.com/sharer/sharer.php?u=http://lmn.9nty.com/?t=PAGETITLE+++-+learnmorenigeria+website%26l=www.learnmoreng.com/site_responcepage.html"> share </a>


但现在的问题是,如何仅通过javascript获取页面标题,然后将其添加到t=PAGETITLE的链接中。

sharer.php
仅将URL作为参数,其余数据将来自:

a href=”http://www.facebook.com/sharer/sharer.php?u=http://lmn.9nty.com/“>共享

顺便说一句,您应该使用共享URL。

我想您只需要在传递到带有
文档.title的FB共享链接的查询字符串中附加一个“.t=”参数。如果这是正确的,你可以这样做

var shareURL = fbShareURL + encodeURIComponent(linkURL + "?t=" + document.title));
这将使URL像

https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Flmn.9nty.com%2F%3Ft%3DTITLE

这将包括页面标题作为url编码形式的查询字符串参数。不过,如何使用查询字符串参数取决于您。使用jQuery,您可以动态更新共享链接:

var fbShareURL = "http://www.facebook.com/sharer/sharer.php?u=";
var title = document.title; // The current page
var linkURL = "http://lmn.9nty.com/";

$link = $("<a>");
$link.attr("href", fbShareURL + encodeURIComponent(linkURL + "?t=" + title));
$link.text("Share");

// Add the link to your page
$("body").append($link);
var fbShareURL=”http://www.facebook.com/sharer/sharer.php?u=";
var title=document.title;//当前页面
var linkURL=”http://lmn.9nty.com/";
$link=$(”


以下是完整的工作HTML代码:

<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>How to get current page title and add it to link directly as variable in JavaScript</title>
  <script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
  <script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){

    // Begin demo code
    var fbShareURL = "http://www.facebook.com/sharer/sharer.php?u=";
    var title = document.title; // The current page
    var linkURL = "http://lmn.9nty.com/";

    $link = $("<a>");
    $link.attr("href", fbShareURL + encodeURIComponent(linkURL + "?t=" + title));
    $link.text("Share");

    // Add the link to your page
    $("body").append($link);
    // End demo code

    });//]]>
  </script>
</head>
<body></body>
</html>

如何获取当前页面标题并将其作为JavaScript中的变量直接添加到链接中
//

不要“呼喊”…这是不礼貌的。您可以使用document.title.获取当前标题,并将其作为变量添加到href=”“@ReubenOdunmbaku您只想获取当前页面标题,并将其作为查询字符串参数添加到JS链接中,对吗?如果是,我想我的答案会对您有所帮助。我知道@Luschn,但我不知道javascript是否可以将页面标题导入href=“”。谢谢您的回复。不,这是不可能的,这正是我在回答中试图告诉您的。您只能使用url作为参数。我仍然相信这是可能的,因为我能够在自己的构建代码中共享我的论坛和url中的自生成标题,那么我认为传递页面标题而不是静态标题是可能的leone.no,sharr.php仅从og:title中获取标题。这是事实;)我试图发布一个片段,但StackOverflow故意阻止Facebook链接,即使在示例中也是如此。道歉。我还能帮什么忙?你看到我的演示链接了吗?我看到了,但无法复制出来,你可以在这里发布完整的代码来帮助我:你好,我已经更新了我的答案,包括一个完整的HTML源代码。非常感谢,但现在的问题是它在我的页脚后追加,我可以让它只追加到我想要的正文部分吗?是的。用页脚元素的jQuery选择器替换
$(“body”)
。例如,如果您的页脚类似于
,则将
$(“正文”)
更改为
$(“#页脚”)
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>How to get current page title and add it to link directly as variable in JavaScript</title>
  <script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
  <script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){

    // Begin demo code
    var fbShareURL = "http://www.facebook.com/sharer/sharer.php?u=";
    var title = document.title; // The current page
    var linkURL = "http://lmn.9nty.com/";

    $link = $("<a>");
    $link.attr("href", fbShareURL + encodeURIComponent(linkURL + "?t=" + title));
    $link.text("Share");

    // Add the link to your page
    $("body").append($link);
    // End demo code

    });//]]>
  </script>
</head>
<body></body>
</html>