1

I want to run a javascript one time. I tried using a script from Dynamic Drive, but it seems to not work with scripts.

I want it to run the first time the user visits the page, but then doesn't pop up again.

Any help?

My script is

    <script src="http://www.google.com/jsapi"></script><script>google.load("jquery", "1");</script> 
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<script type="text/javascript">
    Username="___", <!-- Facebook Username Without http://facebook.com/ -->
    Title="Join Our Facebook!", <!-- Title -->
    Skin="01", <!-- 01 Through 04 -->
    Time="15", <!-- Countdown In Seconds -->
    Wait="0", <!-- Wait For Popup -->
    Lang="en"
</script>
<script type="text/javascript" src="likeboxfbfanpro.js"></script>
<script type="text/javascript">$(document).ready(function(){$().popupbox({ closeable: false });});</script>
5
  • 1
    You know, whenever I come across a site that pops something annoying up, I immediately close the tab and never go to the site again. Everyone I know does the same. Apr 5, 2012 at 22:36
  • I Tried Using A Frequency Cap Script, But It Doesnt Work With Scripts. It Will Work With Text/Images, But When I Put The Script In The Div To Show Once, It Still Pops Up After The First Time Apr 5, 2012 at 22:36
  • 1
    Please Stop Typing Like This It Is Really Annoying To Read.
    – asawyer
    Apr 5, 2012 at 22:37
  • You will need to set a cookie value after you put up the popup and then in your code check to see if the cookie shows the popup has already happened. If not, put up the popup and set the cookie. The cookie value will then be present on future visits.
    – jfriend00
    Apr 5, 2012 at 22:37
  • @jfriend00 Need Help On Doing This. Sorry, Im Not Very Good At This :P Apr 5, 2012 at 22:38

1 Answer 1

3

You can do two things:

  • Set a cookie after the first time you show the message and test for the existence of the cookie on each page load. Show the message if the cookie does not exist or skip it if it does.

  • Set a server side session value (session only), or a database record (spans sessions if you can somehow identify the returning visitor (i.e. IP)) to control the HTML after the first page is served to the client. Only send the message html if there is no session/database value yet

If you want to set cookies. Here's a small script that does the cookie handling:

function createCookie(name,value,days) {
if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
}
else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

Now in your code you can use this as follows:

if (!readCookie('alreadyShown')) {
  .. add you show message code here...
  createCookie('alreadyShown', true)
}
3
  • I would set a cookie, but don't know where to even start. Apr 5, 2012 at 22:48
  • Great answer! Works perfectly for my need. I see the line "date.setTime(date.getTime()+(days*24*60*60*1000));". This is how long until the cookie expires, yes? :) What does this exactly mean? Is this one day, one week or one month? I would like to have one day :) Thanks, @koenpeters
    – zorensen
    Nov 18, 2015 at 7:39
  • The function expects milliseconds. So its days * 24 hours * 60 minutes * 60 seconds * 1000 to get the number of milliseconds Nov 18, 2015 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.