1

I want to include the following code in page.tpl.php only once during first site visit and for subsequently not. I mean this will pop-up an ad only once. I want to set it based on a cookie. How to conditionally include this code based on a cookie? I need the sample code.

<script language="JavaScript">
        var zflag_nid="1185"; var zflag_cid="3307"; var zflag_sid="823"; var zflag_width="1"; var zflag_height="1"; var zflag_sz="15";
        </script>
        <script language="JavaScript" src="http://d8.zedo.com/jsc/d8/fo.js"></script>

EDIT:

Will this work in page.tpl.php ? I want add it before </head> so that the js file is included only once in user session.

<script type="text/javascript">
    if(!jQuery.cookie('myCookie'))
    {
    $.cookie('myCookie':'myValue');
    <!-- Javascript tag: -->
<script language="JavaScript">
var zflag_nid="1185"; var zflag_cid="3307"; var zflag_sid="823"; var zflag_width="1"; var zflag_height="1"; var zflag_sz="15";
</script>
<script language="JavaScript" src="http://d8.zedo.com/jsc/d8/fo.js"></script>
    }

</script>
1

you can add javascripts files using javascript you need to do something like this:

<script type="text/javascript">
  if(!jQuery.cookie('myCookie')) {
    $.cookie('myCookie':'myValue');

    var zflag_nid="1185"; 
    var zflag_cid="3307"; 
    var zflag_sid="823";
    var zflag_width="1"; 
    var zflag_height="1"; 
    var zflag_sz="15";

    //building the script:
    var js = document.createElement("script");

    js.type = "text/javascript";
    js.src =  "http://d8.zedo.com/jsc/d8/fo.js";

    //inserting into the body document
    document.body.appendChild(js);
  }
</script>

Or you can use the jQuery's getScript function: http://api.jquery.com/jQuery.getScript/

You can look more in deep about this here:

https://stackoverflow.com/a/950146

| improve this answer | |
  • Thanks. This code has some small errors and the working one is here: stackoverflow.com/questions/8549616/… . Also Drupal default install does not seem to have cookie plugin so I can't use this code. For including a js file, we can user jquery native method: jQuery.getScript("..js"); – AgA Dec 18 '11 at 4:14
1

This StackOverflow question should contain enough information to solve your issue.

I would go with the one that suggests using setcookie(). $_SESSION information is cleared when the user's session ends, generally at the close of the browser. Here is a recent example of me using cookies to remember an anonymous user's submission of a Webform and redirect them to the completion page if they tried to visit the Webform again:

    <?php
    define('REQUEST_TIME', $_SERVER['REQUEST_TIME']);

    function _mymodule_manage_cookie($nid, $type = 'my_cookie_type') {
      $matches = (bool) $nid == variable_get("mymodule_{$type}_webform_nid", FALSE);
      if (empty($_COOKIE["Drupal_visitor_mymodule_{$type}_submitted"]) && $matches) {
        mymodule_cookie_save(array("mymodule_{$type}_submitted" => TRUE));
      }
    }

    /**
     * Drupal 7 yoink!
     */
    function mymodule_cookie_save(array $values) {
      foreach ($values as $field => $value) {
        // Set cookie for 365 days.
        setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), REQUEST_TIME + 31536000, '/');
      }
    }

    function mymodule_cookie_delete($cookie_name) {
      setrawcookie('Drupal.visitor.' . $cookie_name, '', REQUEST_TIME - 3600, '/');
    }
    ?>

You need to put this in a module or in template.php in your theme. You can conditionally include the code in a manner similar to the following:

template.php

    <?php
    function THEMENAME_preprocess_page(&$variables) {
      // THEMENAME_get_first_visit_cookie() would refer to a function
      // you have to write that retrieves if the cookie
      // is set. You could alternatively try just using
      // $_COOKIE['my_cookie_type'].
      if (THEMENAME_get_first_visit_cookie()) {
        $variables['first_visit'] = TRUE;
      }
    }
    ?>

page.tpl.php

    (skip to wherever you want to embed the code)
    <?php if ($first_viist): ?>
      // Paste your code here
    <?php endif; ?>

Now, one more point. The way I just showed you isn't the best way. You should add your Javascript properly using drupal_add_js() in a module or by adding a separate Javascript file in the theme that reacts to a class on the <body> tag and displays your first visit message (or whatever you're doing). You would still need the $first_visit variable to determine whether to add the class or not.

| improve this answer | |
  • Thanks but I don't want to use code on the server side and instead let me handle in the browser only. This is what I've asked. – AgA Dec 17 '11 at 11:36
  • Yes, then jQuery.cookie should help you. Test it to see if it works. – wizonesolutions Dec 17 '11 at 19:29
0

You are mixing your metaphors so to speak in trying to use jQuery and script tags like php if/thens to include code in your templates.

If you want it all done in the browser, then do it all in the browser, eg, if I haven't been here before, add or execute such and such code to/in the DOM, otherwise don't do anything, eg,

if ( ! // whatever you need to do in javascript to determine if you've been here before) {
  alert("Welcome New User!");
}

in your js.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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