0

There is a link on my menu that send a user to a page where, if he used that specific link, triggers an action (it adds a class to a div element).

Right now I'm doing it with php with a link like : /mypage.php?action=go
and on mypage.php I check with php with : if(isset($_GET['action']) && $_GET['action'] == go) and then the php code is executed

However I would like to do this in a JS equivalent, so instead of php I'd be able to trigger a JQuery action. I'm not very experienced with JS/JQuery, how can I do that properly ?

the script JS script that would be trigger is something along

$("#div1").toggleClass("class-triggered");
3
  • is your question complete? Commented Aug 30, 2016 at 16:57
  • 1
    how about stackoverflow.com/questions/3438326/… Commented Aug 30, 2016 at 16:57
  • @chchrist I will look into this, I missed it because how poorly I was trying to phrase my request, thanks Commented Aug 30, 2016 at 17:01

2 Answers 2

1

Use sessionStorage to have something stateful in client-side :

  • The first page : sessionStorage.setItem('prop1','value1');.

  • The second page includes:

    var getParamValue=function(param) {
           var v = window.location.search.match(new RegExp('(?:[\?\&]'+param+'=)([^&]+)'));
           return v ? v[1] : null;
       }
    if(sessionStorage.getItem('prop1')==='value1' && getParamValue('action')==='go'){
          //
          $("#div1").toggleClass("class-triggered");
    
     }
    

Dont forget to run this code after loading the whole page , specially : #div1.

$(()=>{
    //your code here after load page 
    // Thus, code above should be copy/past here
});
Sign up to request clarification or add additional context in comments.

3 Comments

if he visits the page without the url params the above will still be true.
Thanks for your answer ! How would this work ? is it just sent to the next page or is it conserved client-side for the whole session ? edit : chchrist is right ! this is very likely to happen since the link in question is in the website's menu (in every page)
You could drop the sessionStorage bit. Parsing the query parameters for action=go will do the trick, and it satisfies the desire to keep it in the JS. You wouldn't have to add anything in the PHP code.
0

I think the way you're doing it is the best. If you do anything with cookies or client-side storage, the user has to have that enabled in their settings, and you have to remember to delete the cookie or storage item afterwards.

2 Comments

Indeed, the problem is that it looks very complicated and messy to trigger a JQuery script with the php $_GET['action']
I wouldn't worry about how it looks. Focus on how it works, and keep it simple. Query parameters are transient by nature, and I think that's exactly what you want here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.