2013-11-21 48 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"; 
    } 
}