How to use jQuery onHandler in the presence of require.js

Silver Contributor
Silver Contributor

Hello,

 

has anyone had experience with the onHandler in conjunction with require.js? 

 

We are trying to use the jQuery onHandler extension in TiQ, but it is silently failing. When trying a command on the console, it fails (as is expected) with "Uncaught TypeError: $ is not a function" when not using require().

 

Probably, some kind of call such as this would be needed outside of the code generated by the onHandler Extension:

 

require(['jquery'], function($) { ... })

 

As of now, the only solution seems to be to write a Javascript extension to trigger tracking. Is there a way to get the standard onHandler Extension to work with require.js so that people without JavaScript permissions will again be able to implement Event Tracking?

 

Thanks in advance,

Michael

4 REPLIES 4

How to use jQuery onHandler in the presence of require.js

Tealium Employee

Michael:

 

What do you mean by this statement?

so that people without JavaScript permissions will again be able to implement Event Tracking?

 

This would be tough. I'm assuming that:

 

1. You're trying to prevent jQuery from populating the global namespace so you've required your own build? And in this build you've disabled the global exports of $ and jQuery?

 

 

 

 

 

How to use jQuery onHandler in the presence of require.js

Silver Contributor
Silver Contributor

Hello @Mauricio,

 

exactly, the global exports are not visible, so the onHandler extension fails. Without the onHandler extension, only people with JavaScript permission can then enable tracking by creating a DOM Ready JavaScript extension that wraps the calls in something like "require(['jquery'], function($) {" .

 

I assume this line is fairly standard, so I think it could work if the onHandler extension where modified to have an optional configuration parameter like "use require.js" with an String parameter "require library name" where one could input the "jquery" and then the generated code would have to have the onHandler-code wrapped in a line like above. 

 

This would give the possibilty to use the onHandler by configuration only, therefore also to people not authorized to inject JavaScript.

 

Is that a possibility, or is there another way to achieve this goal?

How to use jQuery onHandler in the presence of require.js

Tealium Employee

@michael_sievers, I'm sorry to say that without permissions for you to use a javascript code extension I cannot propose any workarounds for your problem. If you don't have those permissions, and the site owner has decided not to export jQuery then there's not much you can do from TIQ.

 

The on handler extension relies on the jQuery variable, not $ by the way. you might want to suggest they export jQuery so you can use the onhandler extension if they are not going to give you access to the javascript code extension. 

How to use jQuery onHandler in the presence of require.js

Silver Contributor
Silver Contributor
Hello Mauricio,

that is of course not what I was hoping for, but it does give us a clear path to the solution - thank you!

Best regards,
Michael
Public