2014-01-31 72 views
0

我正在处理一些链接,但无法使其工作! 我会知道它如何使用PHP,但我吸入js:P将一个js变量传递给href

因此,我使用fancybox一些简单的形式。但是我需要将当前文档的URL传递给该页面,所以我们知道填充表单的人来自哪里!每个URL是建立一个像

example.com/city

所以我需要一个城市,这样我就可以把它传递给iFrame的,否则我需要做一个页面为每个城市...

<a class="button fancybox.iframe" id="offerte" data-fancybox-type="iframe" href="offerte.php?page=city">Offerte aanvragen <span>»</span></a> 

类似这样的工作,但我不能以正确的方式进入href!

document.write(document.URL); 

如何获取页面链接并将其传递给href?

+0

网址和重定向到一些新的网址 –

回答

1

您需要使用location.href属性。如果不需要域和协议,那么您可以使用location.pathname

查看更多about Location对象在MDN。

你提到你使用jQuery的在你的标签,因此瓦特/ jQuery的你可以这样做那样:

$('#offerte').attr('href', location.pathname); 

,如果你需要把它作为参数试试这个:

$('#offerte').attr('href', 'offerte.php?page=' + location.pathname); 
+0

感谢您的快速回复!我知道如何得到的网址,但我无法得到它在href – bobbybouwmann

+0

@ user3018238看到我更新的答案 – antyrat

+0

我不认为我解释它正确的方式! – bobbybouwmann

0

更多信息,你可以试试,

document.getElementById("offerte").setAttribute("href",location.href); 
0

请尝试以下代码:

假设网址:example.com/city - > example.com/paris

var u=window.location.href; 
var city=u.substring(u.lastIndexOf("/")+1); 
document.getElementById("offerte").setAttribute("href","offerte.php?page="+city); 
你可以使用`window.location.href`获取当前