我非常喜欢编程而是学习。我有3页,index.htm,ef.htm& dh.htm。在例如我有一个指向ef.htm的链接,并希望,如果你去过ef.htm,当输入index.htm或dh.htm时,你将永远被重定向到ef.htm。反之亦然。 我创建/找到了以下脚本:
<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript" src="/js/cookie.js"></script>
<script type="text/javascript">
$(function() {
var COOKIE_NAME = 'testcookie';
$go = $.cookie(COOKIE_NAME);
if ($go == null) {
$.cookie(COOKIE_NAME, 'test', { path: '/', expires: 10 });
window.location = "/dh.php"
}
else {
}
});
</script>
.js被添加到我的目录中。 如何使该脚本像我想的那样工作? 我尝试了500种不同的东西,但没有成功。还有更优雅的方法吗?我在stackoverflow上找到的东西,不完全是我想要或不工作的东西。
非常感谢你的帮助!
答案 0 :(得分:0)
这是一个可以做你想做的事的例子。
有三个文件:index.html,ef.html和dh.html。只需将代码复制/粘贴到这三个文件中,它就可以完成您想要的操作。
请注意,页面上的大多数javascript / jQuery只是为了让示例正常工作。您真正需要使页面重定向工作的唯一jQuery是:
<script type="text/javascript">
$(document).ready(function() {
fav_site = $.cookie('fav_website');
if (fav_site == 'ef') {
window.location.href ='ef.html';
}else if (fav_site == 'dh') {
window.location.href = 'dh.html';
}
}); //END $(document).ready()
</script>
<强> INDEX.HTML 强>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="//cdn.jsdelivr.net/jquery.cookie/1.3.1/jquery.cookie.js"></script>
<style>
</style>
<script type="text/javascript">
$(document).ready(function() {
fav_site = $.cookie('fav_website');
alert('Cookie Value is now: [' +fav_site+ ']');
if (fav_site == 'ef') {
window.location.href ='ef.html';
}else if (fav_site == 'dh') {
window.location.href = 'dh.html';
}
$('#mybutta').click(function() {
$.cookie('fav_website', 'ef', { path: '/', expires: 10 });
window.location.href = 'ef.html';
});
$('#mybuttb').click(function() {
$.cookie('fav_website', 'dh', { path: '/', expires: 10 });
window.location.href = 'dh.html';
});
}); //END $(document).ready()
</script>
</head>
<body>
<h1>MAIN PAGE (Index.html)</h1>
<input id="mybutta" type="button" value="GoTo Site EF">
<input id="mybuttb" type="button" value="Visit Site DH">
</body>
</html>
<强> DH.HTML 强>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="//cdn.jsdelivr.net/jquery.cookie/1.3.1/jquery.cookie.js"></script>
<style>
</style>
<script type="text/javascript">
$(document).ready(function() {
$('#mybutt').click(function() {
$.cookie('fav_website', '', { path: '/', expires: 10 });
window.location.href = 'index.html';
});
$('#mybuttox').click(function() {
window.location.href = 'index.html';
});
}); //END $(document).ready()
</script>
</head>
<body>
<h1>Page DH DH DH DH DH DH DH DH DH DH</h1>
<input id="mybutt" type="button" value="Reset Cookies (Erase All)">
<input id="mybuttox" type="button" value="Return to Main Page">
</body>
</html>
<强> EF.HTML 强>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="//cdn.jsdelivr.net/jquery.cookie/1.3.1/jquery.cookie.js"></script>
<style>
</style>
<script type="text/javascript">
$(document).ready(function() {
$('#mybutt').click(function() {
$.cookie('fav_website', '', { path: '/', expires: 10 });
window.location.href = 'index.html';
});
$('#mybuttox').click(function() {
window.location.href = 'index.html';
});
}); //END $(document).ready()
</script>
</head>
<body>
<h1>Page EF</h1>
<input id="mybutt" type="button" value="Reset Cookies (Erase All)">
<input id="mybuttox" type="button" value="Return to Main Page">
</body>
</html>