Javascript window.location.replace继续重新加载页面,如何停止?
我正在创建一个脚本,在一些断点处更改CSS,并在两个确切点重定向到另一个页面 更改CSS工作顺利,但重定向点、脚本始终保持重新加载页面,如何停止它并将其设置为仅重新加载一次,如果需要再次重新加载“分辨率再次更改” 代码如下:Javascript window.location.replace继续重新加载页面,如何停止?,javascript,jquery,Javascript,Jquery,我正在创建一个脚本,在一些断点处更改CSS,并在两个确切点重定向到另一个页面 更改CSS工作顺利,但重定向点、脚本始终保持重新加载页面,如何停止它并将其设置为仅重新加载一次,如果需要再次重新加载“分辨率再次更改” 代码如下: /*Change CSS in response to common resolutions.*/ $(document).ready(function() { checkWidth (); $(window).resize(checkWidth); });
/*Change CSS in response to common resolutions.*/
$(document).ready(function() {
checkWidth ();
$(window).resize(checkWidth);
});
function checkWidth(){
var windowWidth = $(window).width(),
cssLocation = $('link[href$="main.css"]');
if (windowWidth <= 240){
cssLocation.attr('href','stylesheets/240-main.css');
}
else if (windowWidth <= 480){
/*Redirecting point 1*/
window.location.replace("smartPhone-index.html");
cssLocation.attr('href','stylesheets/480-main.css');
}
else if (windowWidth <= 768){
/*Redirecting point 2*/
window.location.replace("index.html");
cssLocation.attr('href','stylesheets/768-main.css');
}
else if (windowWidth <= 1024){
cssLocation.attr('href','stylesheets/1024-main.css');
}
else if (windowWidth >= 1280){
cssLocation.attr('href','stylesheets/1280-main.css');
};
};
/*根据通用解决方案更改CSS*/
$(文档).ready(函数(){
支票宽度();
$(窗口)。调整大小(选中宽度);
});
函数checkWidth(){
var windowWidth=$(window).width(),
cssLocation=$('link[href$=“main.css”]”);
如果(windowWidth不使用javascript,请使用
直接在链接标记中:
<link rel="stylesheet" media="screen and (max-width: 240px)" href="stylesheets/240-main.css" />
<link rel="stylesheet" media="screen and (min-width: 241px) and (max-width: 480px)" href="stylesheets/480-main.css" />
<link rel="stylesheet" media="screen and (min-width: 481px) and (max-width: 768px)" href="stylesheets/768-main.css" />
<!-- etc... -->
浏览器将在页面加载和窗口大小调整期间自动加载/更改样式表。通过使用当前页面名称检查目标页面名称
,可以避免多次刷新页面
var xFullPath = window.location.pathname;
var xPageName = xFullPath.substring(xFullPath.lastIndexOf("/") + 1);
function checkWidth(){
var windowWidth = $(window).width(),
cssLocation = $('link[href$="main.css"]');
if (windowWidth <= 240){
cssLocation.attr('href','stylesheets/240-main.css');
}
else if (windowWidth <= 480){
/*Redirecting point 1*/
if (xPageName !== "smartPhone-index.html")
{
window.location.replace("smartPhone-index.html");
cssLocation.attr('href','stylesheets/480-main.css');
}
}
else if (windowWidth <= 768){
/*Redirecting point 2*/
if (xPageName !== "index.html")
{
window.location.replace("index.html");
cssLocation.attr('href','stylesheets/768-main.css');
}
}
else if (windowWidth <= 1024){
cssLocation.attr('href','stylesheets/1024-main.css');
}
else if (windowWidth >= 1280){
cssLocation.attr('href','stylesheets/1280-main.css');
};
};
var xFullPath=window.location.pathname;
var xPageName=xFullPath.substring(xFullPath.lastIndexOf(“/”)+1);
函数checkWidth(){
var windowWidth=$(window).width(),
cssLocation=$('link[href$=“main.css”]”);
if(windowWidth)这试图实现什么?更改CSS文件的href不会导致CSS重新加载,我认为调整大小事件也不会。您需要限制$(窗口)。调整大小()
从外观上看,事件-有一些可以帮助您完成这项工作。更好的方法是对CSS进行编码,使其能够适应所有系统的大小。举个例子,但我必须将cssLocation.attr从第二个if中拉出,以使其完美工作。您太棒了!!谢谢分配!!@MohammedHanafy总是很乐意提供帮助。!
var xFullPath = window.location.pathname;
var xPageName = xFullPath.substring(xFullPath.lastIndexOf("/") + 1);
function checkWidth(){
var windowWidth = $(window).width(),
cssLocation = $('link[href$="main.css"]');
if (windowWidth <= 240){
cssLocation.attr('href','stylesheets/240-main.css');
}
else if (windowWidth <= 480){
/*Redirecting point 1*/
if (xPageName !== "smartPhone-index.html")
{
window.location.replace("smartPhone-index.html");
cssLocation.attr('href','stylesheets/480-main.css');
}
}
else if (windowWidth <= 768){
/*Redirecting point 2*/
if (xPageName !== "index.html")
{
window.location.replace("index.html");
cssLocation.attr('href','stylesheets/768-main.css');
}
}
else if (windowWidth <= 1024){
cssLocation.attr('href','stylesheets/1024-main.css');
}
else if (windowWidth >= 1280){
cssLocation.attr('href','stylesheets/1280-main.css');
};
};