1,105,644 Community Members

Is there a way JS can remember function on new page?

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a simple script (below) that on click (to the element with an id of #addClass) adds a class (.switcher) to the body tag. The script uses jquery.
I would like to know if it's possible to modify this script so that when the user moves on to the next page within the site the added class remains in place? In fact the class needs to stay in place until the element with an id #removeClass has been clicked no matter how many pages the user chooses to browse through. Please be gentle with your responses - I'm not much of a coder! If it's not possible with this script (or indeed with JS) does anyone have any suggestions as to how it may be possible

<script type="text/javascript">
 
    $("#addClass").click(function () {
	  $('body').addClass('switcher');
    });
    $("#removeClass").click(function () {
	  $('body').removeClass('switcher');
    });
</script>

Thanks

Daniel

Member Avatar
Airshow
WiFi Lounge Lizard
2,798 posts since Apr 2009
Reputation Points: 333 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
0
 

Surfsup,

Are these pages hard-coded HTML or dynamically built with server-side scripting?

If the latter, then what?

Airshow

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It's Wordpress site. The header file is sitewide so won't change throughout the site.
Does this help?

Member Avatar
Airshow
WiFi Lounge Lizard
2,798 posts since Apr 2009
Reputation Points: 333 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 11 [?]
Team Colleague
 
1
 

Mmm, the solution lies in saving the switcher false|true state somewhere. There are three basis choices; cookie, PHP $_session var, database. Each has its own scope.

Trouble is, WordPress has its own way of doing just about everything. I would suggest cookie but, due to cookie constraints, that may cut across WordPress's standard use of cookies. You really need a WordPress expert or to get lucky with a search in the WordPress documentation/forums.

(Before someone mentions it, a fourth option is to send the "switcher=0|1" on the url querystring merry-go-round but that would need to involve every internal url in the whole site so don't even think about it unless WP makes specific provision for such).

Sorry, it's not a gentle answer and sorry I can't help further.

Airshow

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks - it sound as though it may be possible - which is a great start! Would you mind posting an code example using of one of the methods you suggest and I'll give it a whirl?

Member Avatar
Surfsup
Newbie Poster
20 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok - so it sounds as though it is possible - however I have little/no chance of solving this without assistance. Can I get any help with this - pretty please... ?
Thanks

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: