javascript - advertisement banner show only once in browser session - Stack Overflow

How do you show an advertisement banner only once during a browser session using JavaScript or jQuery i

How do you show an advertisement banner only once during a browser session using JavaScript or jQuery in a website? Once the session is closed and the browser is closed, the ad must be shown again when I open the website in the browser again. One more thing when i navigate across website banner must display unless it is not closed.

How do you show an advertisement banner only once during a browser session using JavaScript or jQuery in a website? Once the session is closed and the browser is closed, the ad must be shown again when I open the website in the browser again. One more thing when i navigate across website banner must display unless it is not closed.

Share Improve this question edited Jul 3, 2016 at 10:09 user6543514 asked Jul 3, 2016 at 9:50 user6543514user6543514 111 silver badge5 bronze badges 2
  • 1 You should have a look at window.sessionStorage event it is not exactly the behaviour you are expecting – A. Wolff Commented Jul 3, 2016 at 10:22
  • See stackoverflow./questions/29986657/… – guest271314 Commented Jul 3, 2016 at 10:35
Add a ment  | 

4 Answers 4

Reset to default 2

You can use cookie to remember if ad was displayed or not.

You can use this plugin: https://github./carhartl/jquery-cookie

Make a method to display the ad:

showAd() {
 // cookie not set, display the ad and set the cookie
 if($.cookie('adDisplayed') === 'undefined') {
   // code to display the add
   // .....
   // set cookie
   $.cookie('adDisplayed', '1', { expires: 7 }); // set the cookie

 }
} 

To destroy the cookie when user leaves the page, bind beforeunload event

$(window).bind("beforeunload", function() { 
    $.removeCookie('adDisplayed'); 
})

Use sessionStorage

The sessionStorage property allows you to access a session Storage object. sessionStorage is similar to Window.localStorage, the only difference is while data stored in localStorage has no expiration set, data stored in sessionStorage gets cleared when the page session ends. A page session lasts for as long as the browser is open and survives over page reloads and restores.

Opening a page in a new tab or window will cause a new session to be initiated, which differs from how session cookies work.

var banner = document.getElementById('banner');
if (sessionStorage.getItem('set') === 'set') {
  banner.style.display = 'none';
} else {
  sessionStorage.setItem('set', 'set');
}
#banner {
  width: 320;
  height: 50px;
  background: green;
  text-align: center;
}
<div id="banner">Banner</div>

Fiddle Demo

You can play with fadeIn() and fadeOut() methods explained here

like this :

var myVar = setInterval(myTimer, 1000);

function myTimer() {
    $("#banner").fadeOut();
}

https://jsfiddle/bc8jgoga/

You can set a cookie with JavaScript to remember the visit.

Example

<div id="banner"></div>
<script>
  if(typeof $.cookie("banner") === 'undefined' || $.cookie("banner") != "1") {
      $("#banner").html("My Banner");
      $.cookie("banner", "1");
  }
  window.addEventListener("beforeunload", function (e) {
      $.removeCookie("banner");
  });
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492801a4630060.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信