【问题标题】:Using current url in a javascript function [duplicate]在javascript函数中使用当前网址[重复]
【发布时间】:2013-08-03 22:52:05
【问题描述】:

我有这个js函数

function twitterShare(){
    window.open( 'http://twitter.com/intent/tweet?text='+$(".section-title h1").text() +'         
    '+window.location, "twitterWindow",
    "height=380,width=660,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0") 
    return false;
}

当 twitter 窗口打开时,它会抓取 .section-title h1 类,但我需要它说出这样的话,

“在这里查看:http://currentwebsiteurl.com

我需要一个文本字符串,然后获取页面的 url。

任何帮助将不胜感激。

【问题讨论】:

  • “它”?哪个呢?推特?新页面?
  • 是的,我看过了,但不知道如何以正确的方式编写函数。

标签: javascript function url


【解决方案1】:

window.location.href 包含页面的当前 URL。

所以你的函数应该是这样的:

function twitterShare(){
    window.open('http://twitter.com/intent/tweet?text=Check this out here: ' + window.location.href,
                "twitterWindow", "height=380,width=660,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0")
    return false;
}

【讨论】:

  • 感谢 dbanck!像魅力一样工作。
【解决方案2】:

您可以使用window.location.href。它保存您当前所在页面的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 2012-01-01
    • 2014-08-14
    • 2013-10-28
    • 2013-02-23
    • 2014-09-30
    • 1970-01-01
    • 2020-02-17
    相关资源
    最近更新 更多