Я довольно нуб относительно программирования, но обучения. У меня 3 страницы, index.htm, ef.htm & dh.htm. На eg linkedin у меня есть ссылка на ef.htm и хочу, чтобы, если вы были в ef.htm, вы всегда будете перенаправлены на ef.htm при вводе index.htm или dh.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 разных вещей, но не имел успеха. Есть ли еще более элегантный метод? ALl, которые я нашел в stackoverflow, где не совсем то, что я хотел или не работал.
Большое спасибо за вашу помощь!
Вот пример, который будет делать то, что вы хотите.
Существует три файла: 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>
else
части вашего утвержденияif
/then
?