是否可以转到与重新加载的当前window.location.href不同的页面?
我试图在窗口上使用jQuery事件处理程序这样做,但它似乎没有工作。
$(window).on('reload', function(event){
event.preventDefault();
var currentURL = window.location.href;
window.location.href = currentURL.split('!')[0];
});
答案 0 :(得分:0)
假设原始网址为<orig_url>?|<new_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] + "'>");
});
});
答案 1 :(得分:0)
尝试这个。它将解决问题。
$( window ).load(function()
{
var your_url = 'your_page.php';
window.location.href = your_url;
});
答案 2 :(得分:-1)
因为您正在尝试控制客户端的页面加载行为,所以您需要有一些机制来阻止浏览器中的此信息。你可以使用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);
}
})