Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重定向到新页面,但记住原始位置_Javascript_Php_Jquery_Redirect_Cookies - Fatal编程技术网

Javascript 重定向到新页面,但记住原始位置

Javascript 重定向到新页面,但记住原始位置,javascript,php,jquery,redirect,cookies,Javascript,Php,Jquery,Redirect,Cookies,我想在某些条件下将用户发送到“登录”页面,例如 if (condition) { window.location = "http://mywebsite.com/landingpage" } 但我还想记住用户导航到的原始目的地,这样我就可以在登录页上放置一个重定向链接,将用户移动到原始目的地 我知道如何使用PHP实现这一点,但我只需要JS/jQuery。我可以用饼干,如果这能让事情更简单的话 我在想,也许是这样的: // Condition when user moves to

我想在某些条件下将用户发送到“登录”页面,例如

if (condition) {
        window.location = "http://mywebsite.com/landingpage"
}
但我还想记住用户导航到的原始目的地,这样我就可以在登录页上放置一个重定向链接,将用户移动到原始目的地

我知道如何使用PHP实现这一点,但我只需要JS/jQuery。我可以用饼干,如果这能让事情更简单的话

我在想,也许是这样的:

// 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'));
试试这个:

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

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

header('Location: http://mywebsite.com/landingpage?redirect_uri=' .
    urlencode($_SERVER['REQUEST_URI']));
exit();
编辑: 哎呀,你想要JavaScript。谢谢@user2648239!把那个读得快一点。其他人已经用JavaScript进行了回答。

在PHP中:

header('Location: http://mywebsite.com/landingpage?redirect_uri=' .
    urlencode($_SERVER['REQUEST_URI']));
exit();
编辑:
哎呀,你想要JavaScript。谢谢@user2648239!把那个读得快一点。其他人已经用JavaScript进行了回答。

为了安全起见,我会在查询字符串中发送它,并对它进行编码

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

我会在查询字符串中发送它,并且为了安全起见,还会对它进行编码

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

您可以使用
document.referer

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

此链接将重定向到初始页面。

在重定向之前,您可以使用
document.referer

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

此链接将重定向到初始页面。

以下是我解决此问题的方法。它所做的是:如果有人阻止了广告,重定向到一个页面,解释为什么广告对你的网站很重要,然后允许用户导航回他们原来的目的地。由于已设置cookies,该页面将只显示一次

// 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";
    }
}

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

// 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";
    }
}

一种方法是以GETone的方式发送当前位置,另一种方法是以GETAsker明确提到的方式发送当前位置,他知道如何使用PHP。他需要Javascript/JQuery代码。Asker明确提到他知道如何使用PHP。他需要Javascript/JQuery代码。