3

I have a django template page with a youtube video player that looks like this:

<script>
  var tag = document.createElement('script');

  tag.src = "https://www.youtube.com/iframe_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

  var player;
  function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
      height: window.innerHeight - 20,
      width: window.innerWidth - 20,
      videoId: '{{video}}',
      events: {
        'onReady': onPlayerReady,
        'onStateChange': onPlayerStateChange
      }
    });
  }

  function onPlayerReady(event) {
    event.target.setPlaybackQuality('hd1080');
    event.target.playVideo();
  }

  function onPlayerStateChange(event) {
    if (event.data == 0) {
        document.getElementById('choices').style.display = "block";
    }
  }

</script>

<div id="choices" class="choiceBox">
    Which direction?<br>
    <a href="/pedestrian/video/{{ subID }}/{{ condition }}/{{ trial }}/storeChoice/left/">Left</a><br>
    <a href="/pedestrian/video/{{ subID }}/{{ condition }}/{{ trial }}/storeChoice/right/">Right</a>
</div>

It all works and is nothing complicated. I have a div that is hidden until the video stops playing. What I would like to do is get the unix time in milliseconds as soon as one of the 2 hidden links are clicked and store that into a variable. Finally, Id like to attach that as a post parameter in both of the URLs (concatenate ?=time onto the end of the url)

Any idea on how this might be achieved? The issue that I can get the time as soon as the video stops, but it wont add it onto the url presumably because the url has already been written once the page loads the first time. Is it possible to modify that url after the fact?

Simon
  • 9,762
  • 15
  • 62
  • 119

1 Answers1

1

Per your comments, you want to update the href properties of two links at a given point adding the current time as a GET var on the end of the url, then when the user clicks left or right, you want to navigate to the url stored in the clicked element's href property where python code on the receiving page will compare the time stored in the GET var to the then current time. This should work for you:

// I'm going to call modUrls when the window loads
// you could do this wherever appropriate in your code
window.onload = modUrls;

function modUrls(e){
  // get the links
  var left = document.getElementById('left');
  var right = document.getElementById('right');
  //get the time
  var time =  new Date().getTime();
  // append the time page was loaded to the urls in each link
  left.href= left.href+'?time='+ time;
  right.href= right.href+'?time='+ time;

};



//everything below this line is just for testing you can leave it all out  
var left = document.getElementById('left');
var right = document.getElementById('right');
left.addEventListener("click", directionClicked, true)
right.addEventListener("click", directionClicked, true);
function directionClicked(e){
  e.preventDefault();
  
  alert('navigating to: '+this.href)
  window.location = this.href;
  
};
<div id="choices" class="choiceBox">
    Which direction?<br>
    <a id="left" href="/pedestrian/video/{{ subID }}/{{ condition }}/{{ trial }}/storeChoice/left/">Left</a><br>
    <a id="right" href="/pedestrian/video/{{ subID }}/{{ condition }}/{{ trial }}/storeChoice/right/">Right</a>
</div>
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • so that worked exactly as you described. But now I have an issue in my django view. after the user clicks one of those links, it directs them to a view which has the following: trialStart = request.POST['time'] . endTime = int(round(time.time() * 1000)) . trialRT = endTime - trialStart . trialRT then gets stored into a database. The problem is I get a MultiValueDictKeyError error and is complaining about "time"... – Simon Dec 28 '14 at 11:29
  • ultimately what Im wanting is to store the time difference between when the links appear (i.e. the end of the video) and when one of the links get clicked. in other words, how long did it take them to click a link once it appeared – Simon Dec 28 '14 at 11:31
  • In an attempt to solve the error, I change the line to trialStart = request.POST.get('time', False) . but now trialStart returns a false value and the time difference doesnt get calculated properly – Simon Dec 28 '14 at 11:35
  • try changing `POST['time']` to `GET['time']` , the method used here is a the GET method not the POST method. see http://www.w3schools.com/tags/ref_httpmethods.asp – Wesley Smith Dec 28 '14 at 11:36
  • actually looks like with django you should do `request.GET.get('time', '')` see http://stackoverflow.com/questions/150505/capturing-url-parameters-in-request-get – Wesley Smith Dec 28 '14 at 11:42
  • ah yes thats perfect thanks. The difference value that is getting stored seems to be very wrong. I think this may be due to inaccuracies in how the JS method and python methods are getting current time in ms. This isnt helped by the fact that a http request occurs inbetween the 2 time measurements. Is there a better/more accurate way to achieve this? – Simon Dec 28 '14 at 11:43
  • what are the two values you get? – Wesley Smith Dec 28 '14 at 11:45
  • time from GET: 1419766819469. time from python: 1419766819824. which gave me, and stored, a difference value of 355. This is wrong because that is less than half a second, when I waited about 3 seconds between seeing the link and clicking it – Simon Dec 28 '14 at 11:49
  • Ah so you want the GET var to be the time that the links were displayed to the user not the time they clicked the link right? – Wesley Smith Dec 28 '14 at 12:24
  • ah yes, should have explained that properly. the GET var is when the links get shown. the python view will calculate the time the links get "clicked" I put your code within the onPlayerStateChange function, which is why I thought it wouldnt execute until the video stops playing/links get shown – Simon Dec 28 '14 at 12:28
  • I updated my answer, Im updating the urls when the page loads here but you could call the function `modUrls` from anywhere you need. – Wesley Smith Dec 29 '14 at 11:28
  • thanks. I ended up using a hybrid of the different code you provided: get time when user clicks one of the links, used JS to get time as soon as video stops playing (onPlayerStateChange function), then calculate the difference within JS, without passing it onto python on the next few. All seems to work! – Simon Dec 29 '14 at 13:05