11 Replies - 976 Views - Last Post: 16 July 2019 - 07:44 AM Rate Topic: -----

#1 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 8059
  • View blog
  • Posts: 12,461
  • Joined: 28-September 07

Getting Javascript variable value in C#

Posted 11 July 2019 - 09:20 AM

The code:
<script>
var video = document.getElementById("my_video");
$(video).bind("update",function(){
     var whereYouAt = formatTime(this.currentTime);
     var whereYouGo = formatTime(this.duration);
     @{
          string whereAt = <%= whereYouAt%>;
          string whereGo = <%= whereYouGo%>;

          //Output line whereAt/whereGo
       }
)};
function formatTime(seconds){
     var seconds = Math.round(seconds);
     var minutes = Math.floor(seconds/60);
     seconds = Math.floor(seconds%60);
     minutes = (minutes >= 10) ? minutes : "0" + minutes;
     seconds = (seconds >= 10) ? seconds : "0" + seconds;
     return minutes + ":" + seconds;
}
</script>



The question:

How do I (or CAN I) get the values of whereYouAt and whereYouGo from the Javascript and into the C#? Ultimately, I'll be writing them to a database every single time the function is called (hence why the C# is inside the Javascript function). I had come across a Google search result that claimed "<%= varName%>" would allow me to accomplish the task, but it doesn't work.

Any insight, guidance, belittlement, or full-on deranged monologuing about my incompetence is much appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting Javascript variable value in C#

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Getting Javascript variable value in C#

Posted 11 July 2019 - 09:23 AM

You can use AJAX to shovel them into a C# code behind function and that shuffles off the the DB. A PITA but effective.

(barring a full on POST or what not)

Example that I didn't make:
https://stackoverflo...oller-using-url
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7089
  • View blog
  • Posts: 24,090
  • Joined: 05-May 12

Re: Getting Javascript variable value in C#

Posted 11 July 2019 - 11:33 AM

Yes. AJAX to callback to the server.

As you found out, that your hit in Google was mistaken. You can go from C# to Javascript (at page render time) using that syntax. That won't let you go from Javascript back to C# at runtime.

It almost feels like Blazor is a better fit for your project with the amount of interaction you need with C#, but you are too far along this project to change technology stacks now.

(I've not dug too far into Blazor yet, but my gut feel is that it still doesn't solve the problem of seamless communication between client side and server side. Or does Blazor know how to do RPCs back to server side?)
Was This Post Helpful? 0
  • +
  • -

#4 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 8059
  • View blog
  • Posts: 12,461
  • Joined: 28-September 07

Re: Getting Javascript variable value in C#

Posted 11 July 2019 - 11:39 AM

Even if I wasn't so far along, changing technologies is not an option. I have zero input on the technology being used. My boss has dictated it has to be what it is and there is no other option.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Getting Javascript variable value in C#

Posted 11 July 2019 - 11:47 AM

Posted Image
Was This Post Helpful? 1
  • +
  • -

#6 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 8059
  • View blog
  • Posts: 12,461
  • Joined: 28-September 07

Re: Getting Javascript variable value in C#

Posted 12 July 2019 - 08:02 AM

Partially related issue:

Let me explain what I'm trying to do here...
We're using MVC Core 2 (ASP.NET/C#) and Javascript, and connecting to an MSSQL Server.

I have used videojs to create a relatively elaborate video player for a training module.

I have to track specific information about the users of this module:
  • The total duration of the video in question.
  • The furthest point of the video the user has watched.
  • The amount of time the user actually spent watching the video.


The ultimate flow of this system is to do the following:
  • Pull the current session id.
  • Pull the current user id.
  • Pull the current module id.
  • Pull the current component id.
  • Pull the current video id.
  • Determine the total duration of the video.
  • Write a line to the database containing the session id, user id, video id, and video duration.
  • Track the furthest point in the video the user has watched - this is accomplished by using currentTime, writing the user's location in the video to the database record every second or two (however quickly the system can accomplish the commands), but after the first write, it has to pull the existing record and compare the current currentTime to the existing record's currentTime and only overwrite the existing recorded time if the current currentTime is further than the existing record's currentTime.
  • Track the total time watching the video - this will also be using currentTime, but writing the user's time viewing to the database every second or two (however quickly the system can accomplish the commands), but after the first write, it has to pull the existing record and add the latest amount of time elapsed. For example, if the user starts the video and watches the first 30 seconds, then rewinds the video to the 10 second mark and watches the next 20 seconds over again, it will update the existing record to add the 20 seconds to the 30 seconds for a recorded value of 50 seconds. However, if the user jumps forward in the video from the 30 second mark to the 2:30 mark and watches the next 20 seconds, it would still add the 20 to the 30 and update the record to 50 seconds, but not count the 2 skipped minutes.
  • It has to do all of this without an onclick event or a page refresh.


Any ideas on how to accomplish #9 without pulling my hair out in the process?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15300
  • View blog
  • Posts: 61,364
  • Joined: 12-June 08

Re: Getting Javascript variable value in C#

Posted 12 July 2019 - 08:12 AM

Curious - what are you using for a video player?

Side note - this post seems to have a lot of the logistics you are looking for.

https://stackoverflo...l5-video-player
Was This Post Helpful? 1
  • +
  • -

#8 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 8059
  • View blog
  • Posts: 12,461
  • Joined: 28-September 07

Re: Getting Javascript variable value in C#

Posted 12 July 2019 - 10:05 AM

That is awesome, thank you!

And we're using an HTML5 video tag and videojs.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7089
  • View blog
  • Posts: 24,090
  • Joined: 05-May 12

Re: Getting Javascript variable value in C#

Posted 12 July 2019 - 10:11 AM

<joking>
And you my dear woman are an agent of the devil. So what if I can speed read the transcript for the video, and them slam the video location to the right so that I can complete the training in a quarter of the time? Why does it matter if I spent a long or short time watching the video or not?
</joking>

Seriously, though, good luck with this project. It is sounding like a major pain in the a$$.
Was This Post Helpful? 1
  • +
  • -

#10 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 8059
  • View blog
  • Posts: 12,461
  • Joined: 28-September 07

Re: Getting Javascript variable value in C#

Posted 12 July 2019 - 10:33 AM

Your joke was precisely my question. I could open that video and walk out of the room... when it reaches the end of the video, it will claim that I watched the whole thing and I played it for exactly as long as the video duration, and who's going to know that I didn't actually watch the video?

The other fun part about this is the video player has options to play the content at half speed, one and a half speed, and double speed, so I have to account for all of that, too.

And thanks for the well wishes. It's an absolute pain in the rear. It's the new Derpicorn. Except now I don't even get to write it in a language that I know.
Was This Post Helpful? 1
  • +
  • -

#11 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 279
  • Joined: 25-May 16

Re: Getting Javascript variable value in C#

Posted 16 July 2019 - 06:50 AM

I'm no expert on the matter here, but couldn't you do AJAX calls?

Or if you are able to, why not create a rest endpoint such as api/GiveJsValues ?

And in the javascript function, you have a script that does something like
while(!videoComplete)
{
 // call endpoint with your values such as
 // current location in video ie: how far along you are in video
 // whatever else you need to pass in such as userId, sessionId, etc...
}



You can also have an event listener so that if the user clicks out of the page or something you can do one last call to the endpoint so that the DB will be updated with the latest details about the user's progress.

As for knowing if the person walked out of the room and let the video play or not seems a lot like Machine Learning/AI so I am not able to give much input on that yet since I am very new to that.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,246
  • Joined: 12-December 12

Re: Getting Javascript variable value in C#

Posted 16 July 2019 - 07:44 AM

If you included retina recognition you could monitor how long they were staring at the screen...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1