Forum Discussion

jonathancrow's avatar
13 years ago

Open new window if user hasn't seen an article

I would like to create a function where a new window pops up to encourage people to answer the question of the week. But I don't want to overly annoy people.

 

I added the article "Question of the week #1" in the discussion area, then featured the article. I created a custom component on the home page to open a new window to the page of the featured article. The problem is:

 

- I don't want the window to open everytime they visit the home page, only if they haven't seen the window or article yet

- Openning a new window in javascript causes browsers to block the new window

 

Any suggestions?

 

Thanks,

Jonathan

  • AdamN's avatar
    AdamN
    13 years ago

    Hi Jonathan,


    What you're experiencing is likely a data truncation issue with these two lines:

     

    <#assign date_visit = rest("/users/id/" + my_userid + "/last_visit_time").value?datetime("yyyy-MM-dd") />
    <#assign my_topic_posttime = topic.post_time?datetime("yyyy-MM-dd") />

     Because of the "yyyy-MM-dd" timestamp format, it's only picking up the year, month, and date. If you want a more specific date/time you'll need to alter this format (ie. to account for hours, minutes, seconds). Perhaps something like this: "yyyy-MM-dd'T'HH:mm:ssZ"

  • AdamN's avatar
    AdamN
    13 years ago

    Hi Jonathan,

     

    Since the REST API returns the date in GMT+0, I'd suggest getting "now" in GMT+0 as well instead of trying to convert the result to the local timezone.

     

    Here's some sample code I came up with showing how to get the current time in GMT+0 (you'll probably be more interested in the milliseconds version):

     

    <#assign utcDateTime = .now?iso_utc?datetime("yyyy-MM-dd'T'HH:mm:ss") />
    <#assign utcDateTime_number = datesupport.setDate(utcDateTime).millisecondsAsString?number />
    utcDateTime: ${utcDateTime}<br />
    utcDateTime_number: ${utcDateTime_number}<br />

     I hope this helps!

  • I figured out a way to get what I need but am having troubles getting it to work.

     

    I figured if I check the post date of the article and compare to the last visit date I can determine if they need to see the new window.

     

    I posted an article to the "tipsandtricks" board. I featured the article and used the code below.

     

    Here are the values I got from the code:

     

    now is: 1317921100872
    post_time = 1317884400000
    date_visit_number = 1317884400000
    date_visit = Oct 6, 2011 12:00:00 AM
    my_topic_posttime = Oct 6, 2011 12:00:00 AM

     

    I logged out and logged back in and got the following numbers:

     

    now is: 1317921100872
    post_time = 1317884400000
    date_visit_number = 1317884400000
    date_visit = Oct 6, 2011 12:00:00 AM
    my_topic_posttime = Oct 6, 2011 12:00:00 AM

     

    Why didn't the date change?

     

    Why is the last date visit exactly the same as the topic post time?

     

    I posted a new article and got

     

    now is: 1317921370552
    post_time = 1317884400000
    date_visit_number = 1317884400000
    date_visit = Oct 6, 2011 12:00:00 AM
    my_topic_posttime = Oct 6, 2011 12:00:00 AM

     

    Why didn't the post time change?


    CODE
    <!-- Find the current date and convert to milliseconds -->
    <#assign date_now_number = datesupport.now.millisecondsAsString?number />

    <!-- Figure out the current User ID number -->
    <#assign my_userid = user.id />

    <!-- Figure out the last visit date of the current user -->
    <#assign date_visit = rest("/users/id/" + my_userid + "/last_visit_time").value?datetime("yyyy-MM-dd") />

    <!-- Convert last visit date to milliseconds -->
    <#assign date_visit_number = datesupport.setDate(date_visit).millisecondsAsString?number />

    <!-- Find the URL of the featured article in the specific board -->
    <#assign latest_topics = rest("/boards/id/tipsandtricks/threads/featured").threads />
    <#list latest_topics.thread as thread>
    <#assign topic = thread.messages.topic />
    <#assign my_url = topic.@view_href />

    <!-- Find the post time of the featured article -->
    <#assign my_topic_posttime = topic.post_time?datetime("yyyy-MM-dd") />
    <#assign post_time = datesupport.setDate(my_topic_posttime).millisecondsAsString?number />

    <!-- Figure out the date to compare -->
    <#assign my_lastlogin = user.lastVisitTime />

    <!-- For show variable for troubleshooting -->
    now is: ${date_now_number} <br />
    post_time = ${post_time}<br />
    date_visit_number = ${date_visit_number} <br />
    date_visit = ${date_visit} <br />
    my_topic_posttime = ${my_topic_posttime}

    <#if post_time lt date_visit_number>

    <script type="text/javascript">
    function poponload()
    {
    testwindow = window.open("${my_url}", "mywindow", "location=1,status=1,scrollbars=1,width=1000,height=350");
    testwindow.moveTo(150, 350);
    }
    </script>
    <body onload="javascript&colon; poponload()">
    </#if>
    </#list>

    • AdamN's avatar
      AdamN
      Khoros Oracle

      Hi Jonathan,


      What you're experiencing is likely a data truncation issue with these two lines:

       

      <#assign date_visit = rest("/users/id/" + my_userid + "/last_visit_time").value?datetime("yyyy-MM-dd") />
      <#assign my_topic_posttime = topic.post_time?datetime("yyyy-MM-dd") />

       Because of the "yyyy-MM-dd" timestamp format, it's only picking up the year, month, and date. If you want a more specific date/time you'll need to alter this format (ie. to account for hours, minutes, seconds). Perhaps something like this: "yyyy-MM-dd'T'HH:mm:ssZ"

      • jonathancrow's avatar
        jonathancrow
        Advisor
        after taking out the Z after the yyyy-MM-dd'T'HH:mm:ss part it worked like a charm;)
  • Ok. Super cool. Getting this working bit by bit. Decided to go against using a pop-up window as it would get blocked by browsers, and instead created a layer that gets shown if the user has logged in last before the question has been posted.

     

    The problem now is that everytime the page loads during this session the user sees the layer. I want to set the last_visit_date to the current time so that if the user comes to the page again they won't see the layer.

     

    How do I set the last_visit_time of the user? Oh I guess I also need to understand how to post the response to the article...

     

    Btw code is below for fun:

     

    <script type="text/javascript">
    function toggleLayer( whichLayer )
    {
    var elem, vis;
    if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
    else if( document.all ) // this is the way old msie versions work
    elem = document.all[whichLayer];
    else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
    vis = elem.style;
    // if the style.display value is blank we try to figure it out here
    if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
    vis.display = (vis.display==''||vis.display=='block')?'none':'block';
    }
    </script>
    <style>
    div#box {
    border:solid;
    -moz-border-radius: 10px; -webkit-border-radius: 10px;
    border-color:#333;
    -moz-border-radius: 15px;
    padding: 10px;
    /* Firefox 4+ */
    -moz-animation-name: fade;
    -moz-animation-timing-function: linear;
    -moz-animation-duration: 3s;

    /* Webkit */
    -webkit-animation-name: fade;
    -webkit-animation-timing-function: linear;
    -webkit-animation-duration: 3s;

    }
    @-moz-keyframes fade {
    0% { opacity: 0; }
    50% { opacity: 0.5; }
    100% { opacity: 1; }
    }
    @-webkit-keyframes fade {
    0% { opacity: 0; }
    50% { opacity: 0.5; }
    100% { opacity: 1; }
    }

    </style>


    <!-- Find the current date and convert to milliseconds -->
    <#assign date_now_number = datesupport.now.millisecondsAsString?number />

    <!-- Figure out the current User ID number -->
    <#assign my_userid = user.id />

    <!-- Figure out the last visit date of the current user -->
    <#assign date_visit = rest("/users/id/" + my_userid + "/last_visit_time").value?datetime("yyyy-MM-dd'T'HH:mm:ss") />

    <!-- Convert last visit date to milliseconds -->
    <#assign date_visit_number = datesupport.setDate(date_visit).millisecondsAsString?number />

    <!-- Find the URL of the featured article in the specific board -->
    <#assign latest_topics = rest("/boards/id/tipsandtricks/threads/featured").threads />
    <#list latest_topics.thread as thread>
    <#assign topic = thread.messages.topic />
    <#assign my_url = topic.@view_href />

    <!-- Find the post time of the featured article -->
    <#assign my_topic_posttime = topic.post_time?datetime("yyyy-MM-dd'T'HH:mm:ss") />
    <#assign post_time = datesupport.setDate(my_topic_posttime).millisecondsAsString?number />

    <!-- Figure out the date to compare -->
    <#assign my_lastlogin = user.lastVisitTime />

    <!-- For show variable for troubleshooting
    now is: ${date_now_number} <br />
    post_time = ${post_time}<br />
    date_visit_number = ${date_visit_number} <br />
    date_visit = ${date_visit} <br />
    my_topic_posttime = ${my_topic_posttime}
    -->
    <#if post_time lt date_visit_number>
    Now date_visit_number = ${date_visit_number} <br />
    <div id="page">
    <br /><br /><br />
    <div id="box">
    <div style="float:right">
    <input type="reset" name="reset" value="Close Window"
    onclick="javascript&colon;toggleLayer('page');" /></div>
    <p>

     

    Code to grab subject from featured article.

     

     

    Code to post comment to article.


    </p>
    <p>&nbsp;</p>
    </div>
    <br /><br /><br /></div>
    </#if>
    </#list>