1

I need to add a a piece of javascript (a quantast tracking pixel) to the /contacts/index/ page when the user successfully sends a message by completing the contact us form.

Similarrly I need to add another piece of JavaScript code to the /customer/account/index when the user create a customer account, and again for another page where the user books an appointment.

Is there away I could check from a Magento Template file if the user is is on a page which has a success message?

4

The success messages (and all other types of messages) are added by default inside an ul element with the class 'messages'.
You can check via JS if such an element exists in the DOM:

var messageLength = $$('ul.messages').length;
if (messageLength > 0) {
    //there are messages
} else {
    //there are no messages
}

if you want to check only for success messages change the dom selector above from ul.messages to ul.messages li.success-msg.
Each message type is shown in a seperate li element that has a class depending on the message type. success-msg is for success messages.

[EDIT]
I'm not 100% sure about this, but I think you can get the messages stored in the session by using $messages = Mage::getSingleton('customer/session')->getMessages(false). This should give you back an instance if Mage_Core_Model_Message_Collection without removing them from the session. you can later get the success messages from the variable above like this $success = $messages->getItemsByType('success').

But you have to call this code before the messages are displayed in the page, because when they are displayed they are removed from the session.

6
  • I thought of this approach but was trying to load my JS when the DOM loaded not after it loaded. Perhaps it is ok to add it after the DOM loads but my concern was that as the the JS I'm trying to add is a tracking pixel for quantast analytics it should be loaded with the DOM on to be effective.
    – Holly
    Feb 9 '15 at 11:30
  • @CreedBratton. See my edit to the answer. Maybe it helps.
    – Marius
    Feb 9 '15 at 11:37
  • thanks. With your code I was able to check if the message was a success or error message. Brilliant :)
    – Holly
    Feb 9 '15 at 11:50
  • in practice this did not work :(, the Mage_Core_Model_Message_Collection gets the last message & does distinguish if it is active or not so if the user navigates back to the homepage they get the same data. What does the false param mean in getMessages(false) mean?
    – Holly
    Feb 9 '15 at 15:02
  • also $success = $messages->getItemsByType('success') just returned an empty array. Perhaps I'm calling the code too late? but it works with Mage::getSingleton('customer/session')->getMessages(false)->getLastAddedMessage()->getType();
    – Holly
    Feb 9 '15 at 15:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.