Javascript 重新加载时转到其他页面
是否可以转到与重新加载的当前window.location.href不同的页面 我试图用窗口上的jQuery事件处理程序来实现这一点,但它似乎不起作用Javascript 重新加载时转到其他页面,javascript,jquery,Javascript,Jquery,是否可以转到与重新加载的当前window.location.href不同的页面 我试图用窗口上的jQuery事件处理程序来实现这一点,但它似乎不起作用 $(window).on('reload', function(event){ event.preventDefault(); var currentURL = window.location.href; window.location.href = currentURL.split('!')[0]; }); 由于您试图
$(window).on('reload', function(event){
event.preventDefault();
var currentURL = window.location.href;
window.location.href = currentURL.split('!')[0];
});
由于您试图控制客户端的页面加载行为,所以需要有某种机制来防止浏览器中出现这种信息。你可以利用cookie 您可以重用下面的代码
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function DeleteCookie(name) {
document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
$(window).load(function () {
//if IsRefresh cookie exists
var IsRefresh = getCookie("IsRefresh");
if (IsRefresh != null && IsRefresh != "") {
//cookie exists then you refreshed this page(F5, reload button or right click and reload)
DeleteCookie("IsRefresh");
//Redirect to new URL
}
else {
//cookie doesnt exists then you landed on this page for first time
setCookie("IsRefresh", "true", 1);
}
})
函数setCookie(c_名称、值、exdays){
var exdate=新日期();
exdate.setDate(exdate.getDate()+exdays);
var c_value=escape(value)+(exdays==null)?“”:“expires=“+exdate.toutString());
document.cookie=c_name+“=”+c_值;
}
函数getCookie(c_名称){
变量i,x,y,ARRcookies=document.cookie.split(“;”);
对于(i=0;i<0.length;i++){
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf(“=”);
y=ARRcookies[i].substr(ARRcookies[i].indexOf(“=”)+1);
x=x.replace(/^\s+|\s+$/g,”);
如果(x==c_名称){
返回unescape(y);
}
}
}
函数DeleteCookie(名称){
document.cookie=name+'=;expires=Thu,1970年1月1日00:00:01 GMT;';
}
$(窗口)。加载(函数(){
//如果IsRefresh cookie存在
var IsRefresh=getCookie(“IsRefresh”);
if(IsRefresh!=null&&IsRefresh!=“”){
//cookie存在,然后刷新此页面(F5,重新加载按钮或右键单击并重新加载)
删除cookie(“IsRefresh”);
//重定向到新URL
}
否则{
//cookie不存在,然后您第一次登录此页面
setCookie(“IsRefresh”、“true”,1);
}
})
假设原始URL的格式为?|
您可以单击按钮执行此操作
$(document).ready(function () {
$('.btn').on('click', function () {
// Original assumption is the original URL was of the form <orig_url>?|<new_url>
// faking our data here for this example
var currentURL = window.location.href + '|http://www.google.com' ;
console.log(currentURL );
var urls = currentURL.split("|");
console.log( urls[0] );
console.log( urls[1] );
$("head").append("<META HTTP-EQUIV='Refresh' CONTENT='0;URL=" + urls[1] + "'>");
});
});
$(文档).ready(函数(){
$('.btn')。在('click',函数(){
//原始假设是原始URL的形式吗|
//在这里为这个例子伪造我们的数据
var currentURL=window.location.href+'|http://www.google.com' ;
console.log(currentURL);
var url=currentURL.split(“|”);
log(URL[0]);
log(URL[1]);
$(“标题”)。附加(“”);
});
});
试试这个。它会解决问题的
$( window ).load(function()
{
var your_url = 'your_page.php';
window.location.href = your_url;
});
您必须使用卸载事件并检查页面是否已在加载之前加载。你可以在这里看到更多的细节:如果你试图防止双重发布,那么有更好的方法。如果没有,请解释一下你打算怎么做?没有“重新加载”事件。你有重新加载时要去哪里的信息(url)吗?你不应该假装存在url。如果使用条目填充历史记录,请确保应用程序可以加载该页面。否则有什么意义?如果你在其他地方导航并点击浏览器返回按钮,你将无法加载页面。我认为这太复杂了。这就是我尝试这样做的原因:我正在使用history api操纵指向不存在页面的URL。因此,在重新加载时,我希望它转到确实存在的初始页面。@putvande刷新页面时是否使用“文档准备就绪”或“窗口加载”。那么,如果您不同意,为什么要更改答案?@putvande是,我在您第一次发表评论后更改它。