This is a follow up to: Core javascript functions to make error, warning or notice messages appear?
Basically, drupal_set_message() stores error messages in the session. I need a way to tell drupal VIA JAVSCRIPT not to display those messages. If the page was refreshed, these messages would be displayed, but I want a way to force them to be displayed immediately.
I'm not sure if I have understood you correct, but it seems you want to react upon something in your javascript and display a message in drupal style with the effects.
First of all to make things clear, the JavaScript and Drupal (PHP) can't really talk to each other very well. The only solution is through AJAX, so it wouldn't make any sense to want to use drupal_set_message() from your javascript. The reason is that it would be quite difficult and you would just end up with html you would need to append to your page. So it would be much easier to just create the html in your javascript direct, like I proposed in the answer to your other question. So all that would be left would be to append the effects. This is actually not that hard to do, depending on your effects. The only tricky part is getting the settings from the messagefx module, which it seems you currently cant, since it doesn't save any settings in the script variables.
So this solution in code would be something like this:
$("#something").a_trigger(function () {
$("#messages").append('your html with message').find('the html you inserted').effect(some effect here);
});
Depending on the effect you want to create, you would to call a different function, look at what messagefx does in it's module file around line 55.
The other possibility is that you want to react on any drupal_set_message(). That hard part would be to figure out when there is a message, as there isn't really a way to know in the javascript. Also consider that most messages is the result of submitting a form, where the page reload anyways, and this would be unnecessary. So you would start by creating a simple AJAX function to fetch the data, and create your own simple module to do this. Basically all you need to to define an url with hook_menu and in that callback handle the ajax. You can get the messages from the $_SESSION super global.
So the callback, would look something like this
function mymodule_ajax_callback() {
if (isset($_SESSION['messages'] && !empty($_SESSION['messages'])) {
$result = array();
foreach($_SESSION['messages'] as $message) {
$result[] = 'Generate html based on $message';
}
unset($_SESSION['messages']) // Remove the messages so they wont appear twice.
}
return drupal_json($result);
}
The rest will look like the first solution, only you will get the html from the ajax instead.
since these messages are in the session and/or db, you'd have to use ajax i guess. thats not a very conventional approach.
have to say that on my sites this drupal_set_message() function frequently displays the message one page too late, so i feel your pain.
Related
Pretty new here, so sorry if the post isn't clear at first!
I'm trying to get a Custom HTML Tag to work from Google Tag Manager. The tag fires just fine, but the JS code inside the tag doesn't seem to do anything. It's supposed to update an Adroll conversion, but nothing happens.
I'm more wondering if this type of JS would even work in GTM? Like the fact that it uses a specific adroll function, how would that even work without access to a larger library?
<script> try{__adroll.track("pageView", {"segment_name":"eea7cf78"})
} catch(err) {}
</script>
how would that even work without access to a larger library?
It would not. If you have not loaded a library that defines an _adroll object with at least a track method, then this will give an error.
You probably cannot see this error because of the try/catch block. The idea behind try/catch is that you can prevent a script from going into an undefined state bey handling errors in the catch branch in a controlled manner.
In this case however, it is used merely to hide an error. The problem with that is that now you do not know what is wrong :-). Error messages are useful, and suppressing them is not.
There should be an accompanying tracking library, that you can either load via a script tag in the same custom tag, in another custom tag that fires before your tracking tag (best done via tag sequencing), or ideally Adroll would provide a custom GTM template for their tag. At least their support should provide instructions where to get their tracking library.
Also remove the try/catch, since it does nothing but to obscure a problem that should be fixed (and also creates a performance hit in the process).
I am seeking for the proper way to linkify a string block with MeteorJS.
The goal is to prevent XSS vulnerabilities but also to keep the reactivty.
I am currently bulding a chat with MeteorJS. Each of the message in my template can be edited. I have already tried thoses solutions:
1- Linkify DOM node woth various jQuery plugins/custon regex replace() function. Problem is that the onRendered callback is fired once, so messages updated won't be linkified again.
2- Return linkified HTML content from a helper. Problem is that it expose the app to XSS vulneravilities, and with a sanitizing process, some text may be lost (i want to keep the whole text, to display HTML code as text for exanple)
3- Used an autorun conputation declared in the onRendered callback with the Template.currentData() reactive dependency. It ensure that the DOM node will be linkified at every message update, but problem is that old content added manually with jQuery will remain in the node.
So, is there a way to ensure reactivity but also to keep the whole text in place. The goal here is to allow people to send code as text via messages. Meteor Spacebars is already predenting basic XSS with the double curly brace notation {{}}.
Thanks,
wILL
Ok, seems like https://github.com/chuangbo/meteor-marked is the good option. It handle bare links.
I have to mock up a ticking dashboard which is part of a proposal. I am looking for ideas other than the HTTP Refresh option that I am thinking of. The objective is to quickly mock up a look and feel and a working dashboard that ticks over. It only had to provide new content every five seconds. EG there are a bunch of KPI's and their outputs which are percentages have to be updated..
A simple bunch of HTML pages using HTTP Refresh is on my mind. Is there a better option anyoine can think of. EG can HTML5 do this better? Is CSS an option? Thank you in advance for any ideas
I would be going for an ajax call back to the server to get the latest update and then embed that wherever it needs to go - you could set the ajax function on a timer to run every 5 seconds or 1 second or whatever. This way your entire page is not being refreshed, and additionally you can be calling back to the server for a new update even while the previous on is still being rendered.
Downside is that you won't have a page history (i.e. the users will not be able to navigate 'back' to previous ajax updates) unless you explicitly create one; I can't see that being necessary though.
Please post a comment if you need more info about ajax.
In evaluating the Invite and Support modules for Drupal recently, we realized the default tab navigation is not the most user friendly. In particular, each module has a page of tabs that show the various categories of invitations or support tickets (pending, cancelled, etc). For developers, it's easiest to pre-define all the tabs, but from a user standpoint, it makes more sense to only be offered the tabs that contain content.
I assume it's possible to run queries to check which tabs should be displayed for a particular user and change the menus using hook_menu_alter. However, is that the best way to do it or will that cause problems with Drupal's menu caching? Is there a better way?
The answer by jhedstrom is correct, but I'm not that convinced that dynamically showing/hiding local tasks results in better UX, that sounds kinda confusing to me.
Instead, my suggestion would be to use a title callback (which can be added with the same hook_menu_alter() and show the number of things inside that specific tab. That is what I for example use for Privatemsg to show the number of unread messages.
See privatemsg_title_callback() for an example and hook_menu for more information about title callbacks in general.
If you want to selectively remove tabs in a dynamic way (eg, one node gets a tab, while another does not), you won't be able to use hook_menu_alter() because that only has an effect when the menu cache is being built. However, you can do this by overriding the menu access callback. If access to a tab is denied, it won't be displayed.
For example, to override a node tab, you might do something like this:
function mymodule_menu_alter(&$items) {
$item['node/%node/foo']['access callback'] = 'mymodule_override_access';
}
function mymodule_override_access($node) {
// Perform queries, logic, etc to determine if content exists at node/nid/foo.
// Return false if there is no content, otherwise fall through to the original
// access callback function.
}
On stackoverflow, and other websites, if you start making a change to form elements and then you try to navigate away from the page, you will get a confirmation message asking if you are sure you want to discard your changes.
This seems relatively easy to do by hand, but impractical to apply across an entire site. Is there any generic solution that can be plopped onto a page as a control (or even jQuery plugin) which will track IsDirty for all fields (without having to specify each field by hand)?
You can use the window.onbeforeunload event.
See also How can I override the OnBeforeUnload dialog and replace it with my own?
A possiblity would be to clone a selection of all your inputs when the page is loaded (and data into it as well).
You could then do a compare as desribed here:
http://chris-barr.com/entry/comparing_jquery_objects/
Word of warning though, this may be costly, performance wise.