2 loadXMLDoc() in same JS

<script>
    function loadXMLDoc() {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("demo").innerHTML =
      this.responseText;
            }
        };
        xhttp.open("GET", "event.asp?id=1", true);
        xhttp.send();
    }
</script>
onclick="loadXMLDoc()

Can you have multiple onclicks, one for event.asp?id=1 and another is for event.asp?id=2, etc. I am not sure how the code is changed to reflex that.

Thank you.

Try something like this:

onclick="doSomething();doSomethingElse();"

Source: https://stackoverflow.com/a/3910750

I understand that part but how do you alter this script to read the doSomethingElse() ? Meaning have both event.asp?id=1 AND event.asp?id=2. How do you distinguish 1 vs 2 ?

Not 100% sure Iā€™m understanding your question, but Iā€™ll give it a shot. Come back and clarify for me if this doesnā€™t help.

I would start by making your loadXMLDoc function a little more general. If you make the element ID and the endpoint parameters in your function, you can then pass in your own values when you call the function in order to fetch different resources.

<script>
    function loadXMLDoc(elementId, endpoint) {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById(elementId).innerHTML =
      this.responseText;
            }
        };
        xhttp.open("GET", endpoint, true);
        xhttp.send();
    }
</script>

Then, in your onclick attribute, you can call that function twice passing in the appropriate arguments.

onclick="loadXMLDoc('first-element', 'event.asp?id=1');loadXMLDoc('second-element', 'event.asp?id=2');"
1 Like

Brilliant! That works perfectly.

Thank you so much!!

Thank you again for that code which I have in use. I want to track if a user clicks on an embedded video. I donā€™t see how I can utilize this code for that.

Is there in fact a way to track that?

Thanks

Iā€™m not sure what you mean by ā€œtrack if a user clicks on an embedded video.ā€ Do you want to listen for a click event on a video and do something when it is clicked?

I just want to be able to log in my database when someone clicks on play on a youtube video that is embedded on my site. I just couldnā€™t figure out a way to do that and not sure if itā€™s even possible.

This might work for you: http://www.mikesmithdev.com/blog/youtube-video-event-tracking-with-google-analytics/

I know that you can you Youtube to track views and locations. I am a little confused about the Google analytics.js though.

Does this mean you can send data to my previously discussed event.asp page to do your own tracking? I see in the function onPlayerStateChange(event) it mentions 'send" ā€¦ ā€˜Some Labelā€™, etc. Or am I completely misunderstanding that? Thank you.

Do you mean something like this?

<!DOCTYPE html>
<html>
<head>
<title>HTML, CSS and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->

<button id="myBtn">Click Me</button>
  <script>
    var i=0;
function loadXMLDoc(arg) {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("demo").innerHTML =
      this.responseText;
            }
        };
        xhttp.open("GET", "event.asp?id="+arg+"", true);
        xhttp.send();
    }

document.getElementById('myBtn').addEventListener('click',function(){
    if(i>10){
     i=0;
    }
    i++;    
    loadXMLDoc(i);
    
});
  </script>
<!-- End your code here -->
</body>
</html>

Honestly, I am not sure if the code you posted will do it or not.

I can tell you that the above code works great for tracking. What I didnā€™t know if there is a way to take a typical youtube video embed and track if a user has clicked the play button; hence sending that info to the database log which is triggered in the event.asp?id=1 or id=2 code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.