JavaScript save functions when switch pages

I have these two simple functions that change the body background color

<script type="text/javascript">
function lightson(){
    document.body.bgColor="#EBEBEB";
}
function lightsoff(){
    document.body.bgColor="#333333";
}
</script>

using links with onclick to exe the functions

<div style="color:red;">Lights: <a onclick="lightson();" target='_blank' href="#lOn">On</a> - <a onclick="lightsoff();" target='_blank' href="#lOff">Off</a></div>

these append the urls, but when I change them myself to #lOff or #lOn they do nothing, how do I save what the user clicked #lOff or #lOn when they click a page on the site?

I use <body onload="lightson();"> to set the background color on index.php, otherwise it goes to default

Answer:1

To get the url part after the hashtag use

window.location.hash

https://developer.mozilla.org/en/window.location

You could call a function that would check the value of the hash on page load to determine which background colour to use. I'd go for something like this.

window.onload = function(){
   var hash = window.location.hash;

   if(hash == "#lOn"){
      lightson();
   }else{
      lightsoff();
   }
}
Answer:2

I am currently working on an HTML/JS App using Windows Azure Mobile Service. I have two tables, one storing information about an attraction [attractionTable(attractionId, address,...)] and one keeping ...

I am currently working on an HTML/JS App using Windows Azure Mobile Service. I have two tables, one storing information about an attraction [attractionTable(attractionId, address,...)] and one keeping ...

  1. passing query parameters in url
  2. passing query parameters in url angular 7
  3. passing query parameters in url c#
  4. passing query parameters in url node js
  5. passing query parameters in fetch
  6. passing query parameters in curl
  7. passing query parameters in url angular 8
  8. passing query parameters in osb
  9. passing query parameters in postman
  10. passing query parameters to rest service
  11. passing query parameters in url angular 6
  12. passing query parameters in resttemplate
  13. passing query parameters
  14. passing query parameters in mule 4
  15. passing query parameters in angular 2
  16. passing multiple query parameters to rest service
  17. passing multiple query parameters
  18. passing query string parameters in javascript
  19. passing query string parameters in angular
  20. sql query passing parameters

I am using setAttribue as below. Its working only for first time and after that the changing value is showing the alert but not setting with document.getElementById("to").setAttribute("value", ...

I am using setAttribue as below. Its working only for first time and after that the changing value is showing the alert but not setting with document.getElementById("to").setAttribute("value", ...

  1. setattribute not working javascript

I would like to have an automated email sent to a person's email when they press a button. I'm having a few problems, one I am not sure what the JavaScript for this would be. Two, I don't know how to ...

I would like to have an automated email sent to a person's email when they press a button. I'm having a few problems, one I am not sure what the JavaScript for this would be. Two, I don't know how to ...

  1. sending automated emails
  2. sending automated emails outlook
  3. sending automated emails gmail
  4. sending automated emails from salesforce
  5. sending automated emails from google sheets
  6. sending automated emails from excel
  7. sending automated emails from jira
  8. sending automated emails from sql server
  9. sending automated emails using python
  10. sending automated emails from python
  11. sending automated emails using c# windows service
  12. sending automated emails from access
  13. sending automated emails from sharepoint
  14. sending automated emails using java
  15. sending automated emails from access 2010
  16. sending automated emails using c#
  17. sending automated emails from outlook 2010
  18. sending automated emails php
  19. sending out automated emails outlook
  20. sending automatic reminder emails outlook

I know I could use variations of this answer to find out how many different types of data there are in an array: How to count the number of certain element in an array? But, what I am looking for ...

I know I could use variations of this answer to find out how many different types of data there are in an array: How to count the number of certain element in an array? But, what I am looking for ...