2 loadXMLDoc() in same JS


#1

<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.


#2

Try something like this:

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

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


#3

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 ?


#4

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');"

#5

Brilliant! That works perfectly.

Thank you so much!!


#6

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


#7

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?


#8

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.


#9

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


#10

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.


#11

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>

#12

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.


#13

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