XF 2.1 Pass an argument to XF.overlayMessage()'s function?

asprin

Active member
I've this line of code in my javascript

JavaScript:
var foo = "somthing goes here";
XF.overlayMessage('Confirmation', '<p>Are you sure?</p><button class="button button--primary" onclick="XF.hideOverlays(); do_stuff();">Yes</button> <button class="button" onclick="XF.hideOverlays();">No</button>');

I would like to pass foo value as a function argument to do_stuff().

Is this possible?
 
Solution
Your example works fine if you actually use foo as an argument (you aren't in your example):

JavaScript:
var foo = "somthing goes here";
XF.overlayMessage('Confirmation', '<p>Are you sure?</p><button class="button button--primary" onclick="XF.hideOverlays(); do_stuff(foo);">Yes</button> <button class="button" onclick="XF.hideOverlays();">No</button>');

Another way (if you need to pass in the script contents from the PHP code, you could do it like so if you need to have it all self-contained within the message itself):

JavaScript:
XF.overlayMessage('test', $('<script>var foo = "something goes here";</script><p>Are you sure?</p><button class="button button--primary" onclick="XF.hideOverlays();do_stuff(foo);">Yes</button> <button...

digitalpoint

Well-known member
Your example works fine if you actually use foo as an argument (you aren't in your example):

JavaScript:
var foo = "somthing goes here";
XF.overlayMessage('Confirmation', '<p>Are you sure?</p><button class="button button--primary" onclick="XF.hideOverlays(); do_stuff(foo);">Yes</button> <button class="button" onclick="XF.hideOverlays();">No</button>');

Another way (if you need to pass in the script contents from the PHP code, you could do it like so if you need to have it all self-contained within the message itself):

JavaScript:
XF.overlayMessage('test', $('<script>var foo = "something goes here";</script><p>Are you sure?</p><button class="button button--primary" onclick="XF.hideOverlays();do_stuff(foo);">Yes</button> <button class="button" onclick="XF.hideOverlays();">No</button>'))

You need a <script> tag within the message itself, and you need to wrap the whole thing in a jQuery object. Internally if you pass a string (rather then the jQuery object), XenForo uses jQuery.parseHTML() on the string, and by default that method strips out scripts.
 
Solution
Top