Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  • Thread Tools
  • Rate This Thread
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    29
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Adding javascript to my website.

    I am trying to add a script to my website


    Code:
    <script type="text/javascript">
    jQuery(document).ready(function($) {
    
      if (sessionStorage.getItem('advertOnce') !== 'true') {
        $('body').append('<div id="advert-once"><a href="http://www.html5andbeyond.com/session-storage-display-popup-advert/" target="_blank"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/130527/h5ab-logo.png" border="0" alt="banner" /></a><div class="advert-button"></div></div>')
        sessionStorage.setItem('advertOnce', 'true');
      }
    
      $('#advert-once .advert-button').on('click', function() {
        $('#advert-once').hide();
      });
    
      $('#reset-session').on('click', function() {
        sessionStorage.setItem('advertOnce', '');
      });
    
    });
    </script>
    I do not know if I added it right the code works here Display Content / Advert / Popup Once Per Session I am trying to add it to my site. thx

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,303
    Thanks
    30
    Thanked 864 Times in 862 Posts
    It is best to place javascript after the HTML and just before the closing </body> tag. The c ode you posted is jquery so you also need to call that for your code to work Place this line before YOUR <script> tag:
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    Last edited by sunfighter; 05-16-2016 at 11:54 PM.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,950
    Thanks
    211
    Thanked 2,624 Times in 2,602 Posts
    You should use vanilla Javascript for such a simple script, not jQuery with its masssive overhead.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •