admin管理员组文章数量:1356716
I want to send users to a "landing" page on some conditions, e.g.
if (condition) {
window.location = ""
}
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 = "";
}
// 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'));
I want to send users to a "landing" page on some conditions, e.g.
if (condition) {
window.location = "http://mywebsite./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./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'));
Share
Improve this question
edited Nov 21, 2013 at 12:25
Mark Simpson
2,3842 gold badges23 silver badges32 bronze badges
asked Nov 21, 2013 at 12:11
Bram VanroyBram Vanroy
28.7k27 gold badges147 silver badges265 bronze badges
1
- 1 one method is to send the current location as GET – Zword Commented Nov 21, 2013 at 12:13
5 Answers
Reset to default 5You 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.
Try this:
var pathname = window.location.pathname;
window.location = "http://mywebsite./landingpage?url="+pathname;
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./landingpage?loc=" + loc;
}
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./pagetoredirectto";
}
}
In PHP:
header('Location: http://mywebsite./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.
本文标签: javascriptRedirect to new page but remember original locationStack Overflow
版权声明:本文标题:javascript - Redirect to new page but remember original location - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744065880a2584984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论