Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript <;a></a>;在<;的内部自动引用页面url;a></a>;标签_Javascript_Url_Hyperlink_Find_Href - Fatal编程技术网

Javascript <;a></a>;在<;的内部自动引用页面url;a></a>;标签

Javascript <;a></a>;在<;的内部自动引用页面url;a></a>;标签,javascript,url,hyperlink,find,href,Javascript,Url,Hyperlink,Find,Href,如何引用当前页面URL 我在几个页面上有一个共享twitter按钮。我想能够自动引用当前页面的URL,因此当用户单击“推特此页面”时,URL将在推特推特框中生成。我希望这样做的原因是,我可以从一个地方而不是几个页面管理按钮 HTML(当前,url手动粘贴到链接中的href标记中 <a href="http://dangfoods.com/coconutchips.php" title="These chips are great for you HEALTH!! #dangfoods" c

如何引用当前页面URL

我在几个页面上有一个共享twitter按钮。我想能够自动引用当前页面的URL,因此当用户单击“推特此页面”时,URL将在推特推特框中生成。我希望这样做的原因是,我可以从一个地方而不是几个页面管理按钮

HTML(当前,url手动粘贴到链接中的href标记中

<a href="http://dangfoods.com/coconutchips.php" title="These chips are great for you HEALTH!! #dangfoods" class="tweetDialog" target="_blank"><div id="tweets-btn" class="custom-button">Tweet This Page</div></a>

您想使用当前页面URL而不是
href
属性?更改

var loc = $(this).attr('href');

或者,您可以在服务器端生成Twitter共享URL,将其放入
href
属性,然后在onclick stop default handler中,打开带有链接目标的窗口。这样,您的链接就可以在没有JavaScript的情况下工作。

您可以使用“location.href”获取页面的URL,使用“document.title”获取当前页面的标题。请尝试以下操作:

$('a.tweetDialog').click(function(e){
    //We tell our browser not to follow that link
    e.preventDefault();

    //We get the URL of the link
    var loc = location.href;
    //We get the title of the link
    var title  = escape(document.title);

    //We trigger a new window with the Twitter dialog, in the middle of the page
    window.open('http://twitter.com/share?url=' + loc + '&text=' + title + '&', 'twitterwindow', 'height=450, width=550, top='+($(window).height()/2 - 225) +', left='+$(window).width()/2 +', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
});

你的问题是什么?很抱歉@comfrek,我正在尝试引用URL,因此当用户单击twitter按钮时,URL会自动放置到弹出对话框中。目前,对于每个页面,我必须手动将URL放置在href属性中。我希望它能在全局范围内管理一个按钮,而不是每个页面。谢谢对于回复,标题是一项功能,但是location.href没有将链接放在twitter对话框中。如果您访问www.dangfoods.com/coconutchips.php,您将看到我试图实现的目标,但无需手动编辑每个页面上的按钮。这很有帮助,结果只是需要“window.location.href”为了让它工作。谢谢!这很有帮助。结果我只需要放上“window.location.href”。谢谢!
var loc = location.href;
$('a.tweetDialog').click(function(e){
    //We tell our browser not to follow that link
    e.preventDefault();

    //We get the URL of the link
    var loc = location.href;
    //We get the title of the link
    var title  = escape(document.title);

    //We trigger a new window with the Twitter dialog, in the middle of the page
    window.open('http://twitter.com/share?url=' + loc + '&text=' + title + '&', 'twitterwindow', 'height=450, width=550, top='+($(window).height()/2 - 225) +', left='+$(window).width()/2 +', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0');
});