Reputation: 28564
I want to send users to a "landing" page on some conditions, e.g.
if (condition) {
window.location = "http://mywebsite.com/landingpage"
}
but I also want to remember the original destination that a user was navigating to so that I can place a re-redirect link on the landing page which moves the user to the original destination.
I know how to do this with PHP, but I exclusively need JS/jQuery for this. I can use cookies, if that makes things easier.
I was thinking, maybe something like this:
// 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'));
Upvotes: 3
Views: 2963
Reputation: 28564
Here is how I solved it. What it does is: if some one has ads blocked, redirect to a page explaining why ads are important for your website and then allowing the user to navigate back to their original destination. The page will only be showed once because of the cookies that have been set.
// 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";
}
}
Upvotes: 1
Reputation: 637
You can get the url of the page before redirect using document.referrer
var referrer = document.referrer;
window.location = referrer;
This link will redirect to the initial page.
Upvotes: 5
Reputation: 494
In PHP:
header('Location: http://mywebsite.com/landingpage?redirect_uri=' .
urlencode($_SERVER['REQUEST_URI']));
exit();
EDIT: Whoops, you wanted JavaScript. Thanks @user2648239! Read that one a little too quickly. Others have already answered with JavaScript.
Upvotes: 0
Reputation: 586
I would send it on the query string and would also encode it just to be on the safe side.
if(condition) {
var loc=encodeURIComponent(document.location.href);
window.location = "http://mywebsite.com/landingpage?loc=" + loc;
}
Upvotes: 1
Reputation: 6787
Try this:
var pathname = window.location.pathname;
window.location = "http://mywebsite.com/landingpage?url="+pathname;
Upvotes: 1