Set session variable and change page from Jquery? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I specifically need to do things in that order - first set a session variable, but the variable comes from a data attribute in a button. After the variable is set, I want to switch to a new url, where the session variable will be retrieved.
I'm thinking that I need to use Jquery to respond to my button click, but is there a simple way from there to set the session variable and change to another url?
Or should I be looking at another way of doing this?

Session variables are on the server, they have to be set from PHP. So jQuery needs to make an AJAX request to change the variable. It can switch to the new page in the AJAX callback function.
$(".button").click(function() {
$.ajax("something.php", { param: $(this).data("whatever") }, function() {
window.location = "new URL";
});
});

Related

How to generate html page with a link [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How do I implement video keys on my website?
Like when I click a link, it generates an html page containing a specific type of content and gets deleted in certain amount of time.
I haven't done anything code yet.
My only solution is to hard code every html page not generating it.
I don't know how to make those like in YouTube where they can generate "watch?v=[key]" like that
thank you in advance :)
You can use window.open() open new window
const openWidow = ()=>{
var win = window.open("https://www.youtube.com/watch?v=Gjnup-PuquQ", 'window name', config = 'height=500,width=500');
}
also can use window.close() close this window
reference Window.open

jQuery remember option [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have created an option that by clicking on span text shows or hides the sidebar. how to remember user selection after reloading page that the sidebar page was shown or hidden depending on the user's choice?
Once you refresh the page, everything that is not stored somewhere, for example a database, will be lost.
The easiest approach would be to save user's selection to his browser's localStorage.
So, after setting the sidebar to whatever state you want, you can save it like this:
localStorage.setItem('showSidebar', true); // or false
and then, when the page is reloaded, you can get the data like this:
var showSidebar = localStorage.getItem('showSidebar') || false;

How can i change the content of a different page, when my current page is closed? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
On my html page i have a function that is called when the page is closed.
I want this function to change some elements from a different page. Is that possible?
Unless you have a handle to that window because you created it, you'd have to save the information about the modification away somewhere. You could do this by storing the information on the server side. It could also be done purely client side using cookies or local storage.
A very simplistic cookie-based example:
page-to-close.html:
<body onunload="javascript:document.cookie='modified by other page'">
page-to-modify.html:
<script>
var message = document.cookie ? document.cookie : 'first visit'
document.write(message)
</script>

CSS class added via JS to stay during browser session [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Now this added class by onclick is removed after site refresh or when going into subpage. What line is neccessary in my script to have this class remained active during browser session ?
<script type="text/javascript">
function myFunction()
{
document.getElementById('box').setAttribute("class", "contrast");
}
</script>
When you add some classes or elements to your page with JS, it will disappear when you refresh the page, or close/reopen it. It's normal behaviour.
To store information for a longer periods of time, you can use localStorage or sessionStorage:
sessionStorage.setItem will be cleared only when you close the browser, and
localStorage.setItem will store information "forever".
Here is introduction articles:
sessionStorage
localStorage
To store some information, use this:
sessionStorage.setItem('key', 'value');
Then, when your page is loading, you can read stored value and add appropriate class:
var data = sessionStorage.getItem('key');
if (data) {
document.querySelector(yourselector).className = 'yourclass';
}

Pass Variables from Button to Form on Next Page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to pass variables from a button to an input form on another page.
For example,
The user clicks a button with the package they want, the button loads the next page and fill out the form with the package information they have selected. What is the best way to go about this? I am designing the website using Joomla.
Thanks,
Ed
I can suggest you to use localStorage to get this done.
localStorage.setItem('testObject', value);
then get it on other page like this:
var retrievedObject = localStorage.getItem('testObject');
then set the text field with retrievedObject .
see this post to get better idea:Storing Objects in HTML5 localStorage

Categories