使用querystring重定向后在后退按钮上循环 [英] Loop on back button after re-direction with querystring
问题描述
问题:
我需要从一个URL开始,该URL包含一个查询字符串,其中包含第二页的URL-http://www.firstURL.com/?http://www.secondURL.com
.在第一个URL的目标页面上,解析查询字符串以提取第二个URL,然后将浏览器重定向到第二个URL.这是在$(document).ready
上完成的,因此它是自动的.一切正常,但是如果用户单击第二个URL上的后退"按钮,当然会陷入困境.这是基本代码:
I need to start with a URL with a query string containing a URL of a second page - http://www.firstURL.com/?http://www.secondURL.com
. On the target page of the first URL, the query string is parsed to extract the second URL and the browser is re-directed to the second URL. This is done on $(document).ready
so that it's automatic. This all works fine, but of course falls in a hole if the user hits the back button on the second URL. Here's the basic code:
$(document).ready(function() {
var s = location.search;
if(s != '') {
var split = s.split('?');
var loc = split[1].replace('?', '');
location.href = '' + loc + '';
} else {
//do something else on the target page..
}
});
我尝试创建一个条件情况,如果引荐来源网址是第二个URL(上面的代码中的loc
),则重定向不会执行,但似乎在重新链接的情况下方向,则后退按钮不会返回引荐来源网址.
I've tried creating a conditional case where, if the referrer is the 2nd URL (loc
in the code above), the re-direction doesn't execute, but it seems that in the case of a re-direction, the back button doesn't return the referrer.
我必须做所有这些客户端-我无法访问服务器.
I have to do all this client side - I have no access to the server.
是否有某种方法可以防止在单击后退按钮时触发重定向?谢谢.
Is there some way to prevent the re-direction triggering on a back button click? Thanks.
推荐答案
点击第二页后,在浏览器中设置一个cookie,指示已访问第二页. 在第一页中,执行重定向之前,请始终检查cookie是否不存在.
Once you hit the second page, set a cookie in your browser indicating that the second page has been visited. In the first page, before doing the redirection always check whether the cookie is not present.
有关设置cookie的说明:
Instructions on setting a cookie:
<script type="text/javascript">
document.cookie="secondpagevisited=43yj0u3jt;path=/"; //execute this line in the head of second page.
</script>
在第一页中,检查cookie是否存在:
In first page, check for cookie presence:
<script type="text/javascript">
if(document.cookie.indexOf("secondpagevisited=43yj0u3jt")==-1){
/*do redirection here*/
}
</script>
假设您仅控制第一页而不控制第二页,请尝试以下操作:
Assuming you control only the first page and not the second page, try this:
<script type="text/javascript">
if(document.cookie.indexOf("secondpagevisited=43yj0u3jt")==-1){
document.cookie="secondpagevisited=43yj0u3jt;path=/";
/*do redirection here*/
}
</script>
这篇关于使用querystring重定向后在后退按钮上循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!