Triggering JS with Custom Attribute

Hi All -
Sorry for such a newb question but I’m a nurse and this isn’t my primary gig. :slight_smile:

I would like a button on my website to trigger an action using custom attributes, but I just can’t get it to work.

This is the script that I have in the head:

	(function(m,r,a,p,t,w,d){
		m['MRAptWidgetObject']=t;m[t]=m[t]||function(){m[t].q=(m[t].q||[]).concat(Array.prototype.slice.call(arguments))},w=r.createElement(a),d=r.getElementsByTagName(a)[0];w.async=1;w.src=p;w.id=t;d.parentNode.insertBefore(w,d)
	})(window,document,'script','https://widget.medirecords.com/scripts/widget.js','mraptWidget');
	mraptWidget('0804be14-6b34-11e9-b2aa-3f1cb47e6e56', 'MhgRB_-7p4NE-hq9bApBUkB6lK0');

In my custom attributes screen in RW I’ve got a “#” in the URL area and the custom attribute reads as:
Name = onclick
Value = function(mraptWidget)

But it’s not working. I know this might seem simple for you guys but for me I’ve been troubleshooting different combinations for about an hour and not getting anywhere. :frowning:

Any help appreciated!
Cheers,
Chris

Hi @Murse

I can tell you function is a reserved Javascript keyword, so you can’t use that in your current onclick value. It confuses the web browser into looking for a named function named ‘function’. I would look at changing it to this in your link:

Name = onclick
Value = mraptWidget();

That works now in telling the function (named mraptWidget) to run when the link is clicked. Although when that link is clicked, the console is saying:

[MR Appointment Widget] Cannot initialize, no pid/token provided!

So either you have given us a fake pid/token here on the forums or your code is still missing something vital for it to function.

Another thought I’ve had is that this appointment company might be wanting you to pass the pid/token directly as parameters within your onclick function call like this:

Name = onclick
Value = mraptWidget('0804be14-6b34-11e9-b2aa-3f1cb47e6e56', 'MhgRB_-7p4NE-hq9bApBUkB6lK0');

That works in clearing the aforementioned console error, so presumably, the service is now receiving the pid/token it wants to see.

However, I don’t see anything happening on the actual page when the link is clicked. So again I don’t know if this is not working due to a fake pid/token being given, or there is still code missing on the page. We don’t really know what it is supposed to do or have a working example to compare against.

-Will.

Thanks for that Will!
No, all of the code is there and is real…the only thing that’s missing is the begin and end script markers in the code.
All of it seems to work when I just let it run with the script on a webpage. But the moment I try to turn it into a button it doesn’t work.

I’ve tried your suggestions and neither worked. Thoughts?
Cheers,
Chris

So could it be cheaper and easier to setup this functionality on its own page (like an HTML page) and then link to that page using your button or link? Perhaps setting that button or link to open this inside a new tab?

As you probably saw yourself, part of the code is trying to load Javascript from https://widget.medirecords.com/scripts/widget.js, so presumably, there could be something in here that means the functionality only works on page load. It cannot be re-run later on with an onclick event. Especially if it checks pid/token codes and sends data back to your website.

Without a link to any documentation or a working example, I think that is about as far as we can take this. We still don’t know what this code is supposed to do, other than I was able to get it to generate a console error that mentioned appointments.

Thanks again for your thoughts, Will.

If it helps, I have the widget operating on my OLD website. It can be found on the section called “How do I book an online consult?”

Does this help? The website can be found HERE.
Cheers,
Chris

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