2013-11-21 50 views
0

我想在某些情况下将用户发送到“登陆”页面,例如,重定向到新页面但记住原始位置

if (condition) { 
     window.location = "http://mywebsite.com/landingpage" 
} 

,但我也想记住一个用户被导航到,这样我可以将其移动用户到原来的目的地的着陆页上,再重定向链接的原始目标。

我知道如何用PHP做到这一点,但我完全需要JS/jQuery。我可以使用cookies,如果这样做更容易。

我在想,也许是这样的:

// Condition when user moves to the page 
if (condition) { 
    // Set cookie with value of current page 
    $.cookie('locational_cookie', window.location, { expires: 1}); 
    // Redirect 
    window.location = "http://mywebsite.com/landingpage"; 
} 

// When on the landing page, change the href of the "back" link to the original URL that is in the cookie. 
$(".landingpage a.back").attr("href", $.cookie('locational_cookie')); 
+1

一个方法是发送当前位置信息作为GET – Zword

回答

1

试试这个:

var pathname = window.location.pathname; 
window.location = "http://mywebsite.com/landingpage?url="+pathname; 
0

在PHP:

header('Location: http://mywebsite.com/landingpage?redirect_uri=' . 
    urlencode($_SERVER['REQUEST_URI'])); 
exit(); 

编辑: 哎呦,你想要的JavaScript。谢谢@ user2648239!请稍微阅读一下。其他人已经用JavaScript解答了。

+2

提问者已经明确提到,他知道与PHP.He做到这一点,需要使用Javascript/jQuery代码。 – Zword

0

我会发送它的查询字符串,并且也将它编码只是在安全的一面。

if(condition) { 
    var loc=encodeURIComponent(document.location.href); 
    window.location = "http://mywebsite.com/landingpage?loc=" + loc; 
    } 
4

可以使用document.referrer

var referrer = document.referrer; 
window.location = referrer; 

此链接将重定向到初始页面获得页面的重定向之前的URL。

0

这是我如何解决它。它所做的是:如果有人阻止了广告,请重定向到一个页面,解释为什么广告对您的网站很重要,然后允许用户导航回原始目的地。由于已设置的cookie,该页面将仅显示一次。

// When on the landing page, change the href of the "back" link to the original URL that is in the cookie. 
if ($("body").hasClass("page-id-7876")) { // Class of the landing page 
    // Set link 
    $("a.cookie-link").attr("href", $.cookie('locational_cookie')); 
    // Remove locational cookie 
    $.removeCookie('locational_cookie', {path: '/'}); 
    $.cookie('ads_checked', 'true', { expires: 365, path: '/' }); 
} 

if($("#secondary ins.adsbygoogle").is(':empty') || $("#secondary ins.adsbygoogle").height() === 0 || !$("#secondary ins.adsbygoogle").is(":visible")) { 
    $("#secondary ins.adsbygoogle").html('New HTML'); 

    if ($.cookie('locational_cookie') == null && $.cookie('ads_checked') == null) { 
     // Set cookie with value of current page 
     $.cookie('locational_cookie', window.location, { expires: 7, path: '/' }); 
     // Redirect 
     window.location = "http://www.mysite.com/pagetoredirectto"; 
    } 
}