


Use javascript to control cookies to show and hide background images_javascript skills
During major festivals, the homepages of major mainstream websites will be dressed in festive attire. Designers generally use large background images to obtain better visual impact. Of course, some users must also be considered. background image, then it is necessary to place a "Close" button on the background image. As long as the user clicks the "Close Background" button, the large background image will disappear.
We use javascript to control the display and hiding of the background image. When the close button is clicked, the CSS is controlled so that the page does not load the background image. At the same time, COOKIE related parameters are recorded, and the validity period of the cookie is set. Then the page is refreshed within the cookie validity period. The background image will not be loaded again. If the cookie expires, the background image will be reloaded.
HTML
Generally, the close button of the background image is placed at the head of the page. We place the button to close the background at the top of the page. Of course, this button is a ready-made image.
CSS
We also need to prepare a large background image. We find a large background image from the Internet and use CSS to make a simple page layout.
*{margin:0; padding:0}
body{font:12px/18px "Microsoft Yahei",Tahoma,Arial,Verdana,"5b8b4f53", sans-serif;}
#top{clear:both;width:1000px;height:60px;margin:0 auto ;overflow:hidden;position:relative;}
#close_btn{width:60px;height:20px;position:absolute;right:0;bottom:25px;cursor:pointer;
display:block;z-index :2;}
After deploying the css, the page has no effect yet. We need to use javascript to load the background image.
Javascript
When the page is loaded for the first time (no cookies, etc. have been set at this time), of course the background image must be loaded to display the complete page effect. When we click the "Close" button, Javascript will kill the background image that has been loaded on the page, that is, not display it, and set a cookie to control whether the large background image is displayed through the expiration time of the cookie. That is, when the page is refreshed next time, if the cookie has not expired, the large background image will not be loaded. Otherwise, the large background image will be loaded. Please see the code:
$(function(){
if(getCookie("mainbg")==0){
$(" body,html").css("background","none");
$("#close_btn").hide();
}else{
$("body").css( "background","url(images/body_bg.jpg)) no-repeat 50% 0");
$("html").css("background","url(images/html_bg.jpg) repeat- x 0 0");
$("#close_btn").show().css("background","url(images/close_btn.jpg) no-repeat");
}
/ /Click to close
$("#close_btn").click(function(){
$("body,html").css("background","none");
$("# close_btn").hide();
setCookie("mainbg","0");
});
})
Obviously, we control the loading of the background image by setting the CSS background attribute of the page element, and read and set cookies through two custom functions, getCookie() and setCookie().
//Set cookie
function setCookie (name,value){
var exp = new Date();
exp.setTime(exp.getTime() 1*60*60*1000); // Validity period 1 hour
document.cookie = name "=" escape (value) ";expires=" exp.toGMTString();
}
//Get cookies function
function getCookie(name){
var arr = document.cookie. match(new RegExp("(^| )" name "=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Many Windows users have recently encountered an unusual error called Roblox403 Forbidden Error while trying to access website URLs in Google Chrome browser. Even after restarting the Chrome app multiple times, they were unable to do anything. There could be several potential causes for this error, some of which we've outlined and listed below. Browsing history and other cache of Chrome and corrupted data Unstable internet connection Incorrect website URLs Extensions installed from third-party sources After considering all the above aspects, we have come up with some fixes that can help users resolve this issue. If you encounter the same problem, check out the solutions in this article. Fix 1

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

The effects of clearing cookies include resetting personalization settings and preferences, affecting ad experience, and destroying login status and password remembering functions. Detailed introduction: 1. Reset personalized settings and preferences. If cookies are cleared, the shopping cart will be reset to empty and products need to be re-added. Clearing cookies will also cause the login status on social media platforms to be lost, requiring re-adding. Enter your username and password; 2. It affects the advertising experience. If cookies are cleared, the website will not be able to understand our interests and preferences, and will display irrelevant ads, etc.

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.
