Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ATTR funk in IE

I am dynamically creating javascript and attaching it to the onclick event of links on my page using $(document).ready()

My onclick javascript is used to generate event functions that I am passing to Google Analytics to track events on my page, such as clicks on banners and downloaded PDFs. I am getting stats in Google Analytics for these events from every browser except for IE. So, something is wrong with my code in IE (I have searched and searched for errors on my page, there are none).

Normally I would just do something like $("a").click ... do stuff ... but for whatever reason, the only way I could get the Google Analytics event tracking to work was by putting the tracking event function directly on my links. So I'm using the following code to inject the tracking event function into my link's onclick once the page loads....

// Tracks favorites on the home page.
    $("._gatHomePageFavorites").each
    (
        function(index)
        {
            var description = "Content ID: " + getParameterNamedID($(this).attr("href")) + " - " + $(this).children().first().attr("alt");
            $(this).attr("onclick","alert('1');_gaq.push(['_trackEvent', 'Favorites - Home Page', 'Icon Click','" + _gatCleanString(description) + "']);alert('2');");
        }
    );

I think my problem is that IE is not putting my code on the onclick. But I don't know of a good way to view the generated source in IE. I have tried a couple of javascript functions in the address bar to bring up the generated source, assuming they work, then my code is not injecting the tracking event function into my link's onclick for IE. I see the tracking event function in the onclick in Firefox's view generated source.

As another test, you can see I added alerts around my tracking event funciton. In FF both alerts trigger, In IE neither alert triggers.

One more piece of info. My Google Analytics is not recording events for any IE browser. As far as I can tell, my issue is not specific to one version of IE.

How can I tell if my dynamic javascript is getting into the onclick for IE, and then what can I do to get it into the onclick for IE?

UPDATE To simplify the problem and to focus the direction of the answers, I removed the Google Analytics event function. Now, all I am doing is injecting alert() into the onlick. IE won't even trigger the alert(). I have tried the following...

// Tracks favorites on the home page.
    $("._gatHomePageFavorites").each
    (
        function(index)
        {
            $(this).attr("onclick","alert('1')");
        }
    );

and

// Tracks favorites on the home page.
    $("._gatHomePageFavorites").each
    (
        function(index)
        {
                $(this).attr("onclick","setTimeout(\"alert('1')\", 1000);return false");
        }
    );

So I'm still leaning towards my javascript is not being injected into the onclick in IE.

What is the most reliable way to view generated source in IE?

If I can confirm that my code is not being injected into the onclick attribute of the link, then I can at least have an answer as to why Google Analytics isn't tracking events for IE. It would be because my injected code does not exist in IE.

like image 976
user1132517 Avatar asked Dec 04 '25 13:12

user1132517


1 Answers

You should not be adding the 'onclick' attr, but rather using this using jQuery .click() event.

function(index){
    var description = "Content ID: " + getParameterNamedID($(this).attr("href")) + " - " + $(this).children().first().attr("alt");
    $(this).click(function() {
        alert('1');
        _gaq.push(['_trackEvent', 'Favorites - Home Page', 'Icon Click',  _gatCleanString(description)]);
        alert('2');
    });
}

something like above, sorry wrote this quick, so might have a typo.

like image 139
Jakub Avatar answered Dec 07 '25 02:12

Jakub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!