Sunday, 30 March 2014

window.onbeforeunload ajax request problem

From last few hours I was facing a problem with window.onbeforeunload. My need was to save form data(partial saving) by ajax, in case user opts to close the browser, reload the window or close the window. I was using following code :

//Old Code
window.onbeforeunload = function () {
  return msg;

It was saving my content but the return statement was showing me a confirmation message with options ‘Leave Page’ and ‘Stay on Page’. It was so embarrassing. Without return statement it was not saving my data at all.

After spending lot of time on internet and brainstorming a little found the following solution:

//New Code

$(window).bind('beforeunload', function () {
                //this will work only for Chrome

$(window).bind("unload", function () {
 //this will work for other browsers

It worked like a charm for me. Hopefully it will help someone else as well on internet.

Thanks!!!!! Enjoy Programming :)

Sunday, 23 March 2014

Magento : Add “Subscribe to Newsletter” checkbox on Contact Form

To add "Subscribe to Newsletter" checkbox on Contact form, follow following steps: 

1) Go to contacts/form.phtml file of your selected theme.

2) Place following code in the file as per your requirement :
    <input type="checkbox" name="subscribe_newsletter"><?php echo    
     Mage::helper('contacts')->__(' Subscribe to Newsletter ') ?>

3) Now go to \app\code\local\Mage\Contacts\controllers\IndexController.php file 
    and put following code in postAction() function :

    Note: If you are not able to find Mage/Contacts.... folder in local directory please create new one and refer to the link (

/*Start of code*/


                        $email = $_POST['email'];
                        $status = Mage::getModel('newsletter/subscriber')->subscribe($email);
                        if ($status == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
Mage::getSingleton('customer/session') ->addSuccess($this->__('Confirmation request has been sent.'));
Mage::getSingleton('customer/session') ->addSuccess($this->__('Thank you for your newsletter subscription.'));

       /*End of code*/

That’s it and you are done. Hopefully it will help you guys as well.

Thanks!!!!!!!! Enjoy Programming J


Sunday, 16 March 2014

Magento 1.7 not showing Google Analytics code in application.

Recently, faced an issue with Google Analytics code implementation in Magento version 1.7. Done with all the settings to implement Google Analytics, still it was not showing Google Analytics code in my Magento application.

Searched a little and came up with following solution.

Go to \app\design\frontend\default\mytheme\layout\googleanalytics.xml file :

<reference name="after_body_start">
        <block type="googleanalytics/ga" name="google_analytics" as="google_analytics" />

Code Block 

<reference name="after_body_start">
        <block type="googleanalytics/ga" name="google_analytics" as="google_analytics" 
            template="googleanalytics/ga.phtml" />

Above code worked for me. Hopefully it will work for others as well.

Thanks!!!!!!!!! Enjoy Programming :)