如何加载页眉,更改它,然后重定向到具有更新页眉的页面
How can I load a header, change it, then redirect to a page with the updated header?
我有3个html文档:header.html
、blackPage.html
和greenPage.html
。CCD_ 4对于CCD_ 5和CCD_。我想使用一个名为HeaderScript.js
的脚本,使标题中用于更改页面的按钮反映当前页面的颜色。我该怎么做?(这只是一个例子:在实际应用程序中,绿色页面上没有足够的信息来确定标题应该是什么,所以我不能在加载绿色页面时更新它)。
以下是我正在使用的4个文件:
header.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src=HeaderScript.js></script>
<script>
$(document).ready(function(){
$("#Button").click(function(){
changePage($("#Button").get(0));
});
});
</script>
<style>
#Button {
height: 80px;
width: 80px;
background-color: black;
}
</style>
<div id="Button"></div>
</html>
blackPage.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#Header").load("header.html");
});
</script>
<Title>Black Page</Title>
<h>Black Page</h>
<div id="Header"></div>
</html>
greenPage.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#Header").load("header.html");
});
</script>
<Title>Green Page</Title>
<h>Green Page</h>
<div id="Header"></div>
</html>
HeaderScript.js
function changePage(div){
if (div.style.backgroundColor === "green"){
div.style.backgroundColor = "black";
alert("Color changed to black");
document.location.href = "blackPage.html";
}
else{
div.style.backgroundColor = "green";
alert("Color changed to green");
document.location.href = "greenPage.html";
}
}
新header.html
页面代码:
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script>
<script src="HeaderScript.js"></script>
<script>
$(document).ready(function(){
var path = window.location.pathname;
path = path.substr(path.lastIndexOf('/') + 1,
path.indexOf('Page.html') - path.lastIndexOf('/') - 1);
$("#Button").css('background-color', path);
$("#Button").click(function(){
path = (path == 'green' ? 'black' : 'green');
$("#Button").css('background-color', path);
window.location = path + 'Page.html';
});
});
</script>
<style>
#Button {
height: 80px;
width: 80px;
}
</style>
<div id="Button"></div>
</html>
希望奏效;)让我知道。。
ThatWeirdo的答案是有效的,他的答案在页面更改之前加载颜色(与此解决方案相反),但对于这个问题只是一个例子的特定问题,我需要采取不同的做法(我的原始代码是垃圾,所以我制作这个例子是为了让它更简单/更清晰)。
以下是我最终要做的事情(假设blackPage.html总是加载的第一个页面):
header.html
<html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src=HeaderScript.js></script>
<script>
$(document).ready(function(){
//--ADDED--
var buttonColor = localStorage.buttonColor;
if(buttonColor !== undefined){
$("#Button").css("background-color", buttonColor);
}
//----
$("#Button").click(function(){
changePage($("#Button").get(0));
});
});
</script>
<style>
#Button {
height: 80px;
width: 80px;
background-color: black;
}
</style>
<div id="Button"></div>
</html>
HeaderScript.js
function changePage(div){
if (localStorage.buttonColor === "green"){ //CHANGED
localStorage.buttonColor = "black"; //CHANGED
alert("Color changed to black");
document.location.href = "blackPage.html";
}
else{
localStorage.buttonColor = "green"; //CHANGED
alert("Color changed to green");
document.location.href = "greenPage.html";
}
}
相关文章:
- 通过javascript重定向html传递php变量
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 使用angular重定向到html页面
- 在不重定向页面的情况下更新表
- 表单在每个页面中 - 仅在特定页面上处理它 - 如果当前不在页面上 - 重定向 - 否则 - 使用 AJAX 更新数据
- 异步mongodb更新循环中最后一次迭代后的重定向
- 如何进行服务器端HTTP重定向更新引用程序(更改原始引用程序)
- 页面重定向后,使用选定的选项文本更新下拉菜单文本
- 已更新:从另一个页面重定向 url 传递参数
- 页面未重定向到更新的网址
- Django提交表单->重定向到新页面->自动更新新页面
- js窗口.重定向不断更新
- 如何在jquery中更新动态标签后重定向第二个html页面
- 如何在被重定向到另一个页面后更新所选项目
- JQuery读取XML文件,然后在atrr=更新时重定向
- 不使用HTML 5更新查询字符串(没有重定向)
- 在响应之前更新aspx页面上的标签.重定向
- 如何加载页眉,更改它,然后重定向到具有更新页眉的页面
- 如何更新和重定向到以前的页面使用javascript
- 当尝试通过链接加载内容时,Jquery load不工作,而是重定向而不是更新