0

I created one JS button on Lead detail page where onClick javascript is written as below-

{!REQUIRESCRIPT('/soap/ajax/26.0/connection.js')}
{!REQUIRESCRIPT('/js/functions.js')}
{!REQUIRESCRIPT('/resource/jQuery/jquery-1.8.2.min.js')}
{!REQUIRESCRIPT('/resource/jQuery/ui/jquery-ui-1.9.1.custom.min.js')}

requireCssFile('/resource/jQuery/ui/css/ui-lightness/jquery-ui-1.9.1.custom.min.css');

{!REQUIRESCRIPT('/resource/jQuery/postmessage/jquery.ba-postmessage.js')}
{!REQUIRESCRIPT('/resource/jQuery/bbq/jquery.ba-bbq.min.js')}

function requireCssFile(filename) {
    var fileref = document.createElement('link');
    fileref.setAttribute('rel', 'stylesheet');
    fileref.setAttribute('type', 'text/css');
    fileref.setAttribute('href', filename);
    document.getElementsByTagName('head')[0].appendChild(fileref);
}

var j$ = jQuery.noConflict();

var iframe_url = 'https://devapp.alyce.com/remote/v1/iframe/gift-create?gcfn={!Lead.FirstName}&gcln={!Lead.LastName}&gce={!Lead.Email}';

var user="select Alyce_Refresh_Token__c FROM User WHERE Id = : UserInfo.getUserId()";

if(user!=null){
  iframe_url+='&rt={!User.Alyce_Refresh_Token__c}';}

iframe_url+='&ru=https://c.ap4.visual.force.com/apex/CreateGiftPage?LeadId={!Lead.Id}';
var child_domain = iframe_url.substring(0, iframe_url.indexOf('/', 9));
var parent_domain = window.location.protocol + '//' + window.location.host;
var j$modalDialog = j$('<div></div>')
       .html('<iframe id="iframeContentId" src="' + iframe_url + '&parent_domain=' + parent_domain + '" frameborder="0" height="100%" width="100%" marginheight="0" marginwidth="0" scrolling="yes" />')
       .dialog({
            autoOpen: false,
            title: 'Alyce',
            resizable: false,
            width: 800,
            height: 500,
            autoResize: true,
            modal: true,
            draggable: false
});
j$modalDialog.dialog('open');

j$.receiveMessage(
    function(e){
 j$modalDialog.dialog('close');
console.log('test4'+cross_result);
        var cross_result = j$.deparam(e.data);
console.log('test4'+cross_result);
        if (cross_result.action == 'close_modal_dialog') {
            j$modalDialog.dialog('close');
        }
    },
    child_domain
);

I am calling the following VF page-

<apex:page Controller="CreateGiftButtonPageExtension" action="{!toReceiveParameters}" showHeader="false" sidebar="false">
<apex:pageMessages ></apex:pageMessages>
  <head>
    <apex:includeScript value="{!URLFOR($Resource.jQuery, '/jquery-1.8.2.min.js')}"/>
    <apex:includeScript value="{!URLFOR($Resource.jQuery, '/postmessage/jquery.ba-postmessage.js')}"/>

    <script type="text/javascript">
        var j$ = jQuery.noConflict();

        var parent_domain = '{!JSENCODE($CurrentPage.parameters.parent_domain)}';

        j$(document).ready(function() {
            j$('input[id$=btnCloseModalDialog]').click(function(event) {
                event.preventDefault();
                closeModalDialog();
            });
        });

        function closeModalDialog() {
            var cross_result = new Object();
            cross_result.action = 'close_modal_dialog';

            j$.postMessage(
                cross_result,
                parent_domain,
                parent
            );
        }
    </script>
</head>
<apex:form >
    <apex:commandButton id="btnCloseModalDialog" value="Close modal dialog" />
</apex:form>
</apex:page>

By doing so, I able to open the popup window but by clicking on command button the popup is not closing.

Any valuable suggestion will be appreciated. Thank you.

6
  • Check in console if there are any javascript errors? – Ashwani Mar 22 '17 at 11:43
  • by doing so i have i found that closeModalDialog() is successfully calling. but receiveMessage() in js button is not getting called. – p.samruddhi Mar 22 '17 at 11:48
  • Is it showing some errors or warning when you click the button. Looks like it has issue in calling postMessage() – Ashwani Mar 22 '17 at 11:51
  • warning is there-Uncaught SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode – p.samruddhi Mar 22 '17 at 11:53
  • 1
    try after writing "use strict"; in beginning of script tag and in beginning of button script – Ashwani Mar 22 '17 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.