Forum Discussion

jgroves's avatar
jgroves
Expert
11 years ago

javascript to execute before post

Hi,

I'd like some javascript to execute when a user clicks "post" when either begining a thread or replying. Basically for many complicated reasons (too much detail to provide in this post) we're needing to check the post for a certain character set and if the post or title contains this character set we don't want to allow the user to post. I know the Lithium out of the box content filters do forms of this, but for numerous reasons that will not meet our needs. Does anyone know how I can make this javascript execute upon the "post" action? I'm unsure which API call is best.

 

Thanks,

Jordan 

5 Replies

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)
    11 years ago

    jgroves 

     

    I know you have your reasons to not use the out of the box filters, but I would advise to reconsider it. What you're looking to do seems very easy with a regular expression and the filters ...

     

    Now, if you really want to use some scripting (which might slow down the system), have you tried to look at jQuery? jQuery is quite powerful when it comes to forms and actions ... Just a thought ...


  • jgroves wrote:
    I'd like some javascript to execute when a user clicks "post" when either begining a thread or replying.

    This is very possible.


    jgroves wrote:
    Basically for many complicated reasons (too much detail to provide in this post)

    Ahh the bane of the online forum.This is wildly off topic, but the reason this one gets in my way is that if I knew what problem you were trying to solve there might be a better way to do it. Like when the team who posts in our Blog spent weeks putting together a brief and then sent me a big pack on the feature they needed to develop for an essential tool. Until I pointed out we could just add a component for it.

     

    But that's ok, let's solve the problem in front of us :)

    The easiest way to do what you want is going to be to make your own custom components for the reply and post buttons.
    Use CSS to hide the existing buttons and place yours there via studio.

    Then write your javascript to check the post div on page load (why wait til it's clicked right :) ) and then either make the button generate an alert on click or function as normal.

    You'll have to make sure you wrap the javascript in the Lithium wrapper so it queues up with the rest and executes after the page is ready.

     

    I'd code it so it executes like this.

     

    1. Page loads as normal, the post and reply buttons point to the reply page by default (this is your fallback for people without javascript enabled).

    2. After the page is "ready" the code does a quick check of the right div and looks for the characters in question.

    3. if it finds them it switches the innerHTML for the button to do an alert, or changes the button to disabled and shows a warning div.

    4. if it doesn't find it, it does nothing.

     

    This would cover you if the characters are found, if the characters are not found, and most of all if there is no javascript.

    It's not perfect, it's a little clunky, and with javascript disabled people can still post.

    It'll get the job done.

     

    And leave me WILDLY curious what kind of content you're allowing to be posted but not replied to!

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)
    11 years ago

    Tim_h 

     


    Use CSS to hide the existing buttons and place yours there via studio.

    Then write your javascript to check the post div on page load (why wait til it's clicked right :) ) and then either make the button generate an alert on click or function as normal.

    You'll have to make sure you wrap the javascript in the Lithium wrapper so it queues up with the rest and executes after the page is ready.

     

     


     

    We have to keep in mind that by hidding the buttons, using CSS, the end user can always use the developer tools / FireBug on their web browser to make them visible again, and therefore post the character set they want ... ;-)

  • Tim_h's avatar
    Tim_h
    Boss
    11 years ago

    OlivierS wrote:

     

    We have to keep in mind that by hidding the buttons, using CSS, the end user can always use the developer tools / FireBug on their web browser to make them visible again, and therefore post the character set they want ... ;-)


    Sounds like I'm not the only one who constantly uses the developer console to touch things on webpages.

    So many HTML comments to be found ;)

  • phani's avatar
    phani
    Advisor
    11 years ago

    Hi Jordan,

     

    The following jquery code might fullfill your needs. 

     

    $('.lia-button-Submit-action').bind('click', function(e) {

    /* perform your validation here */
    var subject = $(".lia-subject");
    if(subject.value.match(/* characters to check */))
    {
    e.preventDefault();
    alert(/* alert user for wrong characters */)
    }else {
    return true;
    }
    };

     

    I have given example to check subject after user click Post button.