Question

I need help creating a simple if else statement that will help us track outbound links.

The goal is to return a true or false (or 1 or 0) whenever the URL being clicked matches the current hostname/domain. Here's what I have so far:

<script>
function myFunction()
{
var x = document.getElementsByTagName("A")[0].hostname;
var y = location.hostname;
    if (x=y) {
        return 1;
    } else {
        return 0;
    }
}
</script>

Obviously I have no idea what I'm doing, so I appreciate the help. I'm eventually going to be using this as a macro in Google Tag Manager for tracking outbound links.

Thanks,

Was it helpful?

Solution

function myFunction(){
    var x = document.getElementsByTagName("A")[0].hostname;
    var y = location.hostname;
    return x === y; // returns true/false
}

In if statement two or three equal are for comparisons, not one

= assignment == equality === strict equality ( type )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top