0

Hey

I want to declare a variable that is global that I can use (and modify) all over my website. Ive read that I may be able to do it with jQuery but not sure how to (more importantly, where).

Its a simple text field, nothing else. Specifically it must be able to be accessed in Javascript, PHP and phtml (which is PHP allowed with HTML right?)

Thank you!

5
Contributors
8
Replies
10
Views
5 Years
Discussion Span
Last Post by fobos
0

You could try making a script and insert it on all your pages. In php, you would have to write the session on top of every page, so why not include a script on every page.

<script type="text/javascript">
$(document).ready(function(){
  $("a").click(function(){
    // your global variable
    var z = "cheese";
    //gets the href attribute
    var x = $(this).attr("href");
    //go to links location with variable
    location.href=x+z;
  });
});
</script>
<body>
<a href="www.google.com/#q=">click me</a>
</body

Hope this helps

0

how about using $_SESSION ?? http://w3schools.com/php/php_sessions.asp

But in a Javascript context (well or HTML, depends) I cant store a PHP session variable can I?

You could try making a script and insert it on all your pages. In php, you would have to write the session on top of every page, so why not include a script on every page.

Hope this helps

I already have a Javascript which I execute when I click the button that interacts (so to speak) with the name. I tired to use "document.cookie" but obviously it only applies to the CURRENT page, not any others.

Also, if I do indeed load a script on every page, besides the penalty in loading it, I would have to make sure that I dont overwrite the variable each time I load it right? I just use it on ONE page (and it is opinional)

0

Cookies, local storage, indexDB, WebSQL, you can use any of these to keep persistent data accross your site.

I'd suggest cookies or local storage, you will have browser issues with indexDB and WebSQL and as long as it's not a lot of data.

0

Cookies, local storage, indexDB, WebSQL, you can use any of these to keep persistent data accross your site. I'd suggest cookies or local storage, you will have browser issues with indexDB and WebSQL and as long as it's not a lot of data.

Not really sure whats the difference between Cookies and local storage :P but I dont mind either way.

My main object right now is to

1: Clicking a button that executes Javascript, inside a Javascript function, store a value in a Cookie (or local store)
2: In a completely different PHTML page, have it show that variable's value.

Thank you for helping :)

0

use jquery cookie plugin, it's easy to set and retrieve a cookie. https://github.com/carhartl/jquery-cookie

it's almost 2kb only.

Set an expiring cookie:

$.cookie('the_cookie', 'the_value', { expires: 7 });

Set a session cookie:

$.cookie('the_cookie', 'the_value');

Retrieve that cookie:

$.cookie('the_cookie');

Delete that cookie:

$.cookie('the_cookie', null);

So simple yet effective. You can store any values you want to that cookie, you could reset it's value too.

Edited by lambing

0

use jquery cookie plugin, it's easy to set and retrieve a cookie. https://github.com/carhartl/jquery-cookie it's almost 2kb only. Set an expiring cookie: $.cookie('the_cookie', 'the_value', { expires: 7 }); Set a session cookie: $.cookie('the_cookie', 'the_value'); Retrieve that cookie: $.cookie('the_cookie'); Delete that cookie: $.cookie('the_cookie', null); So simple yet effective. You can store any values you want to that cookie, you could reset it's value too.

(Javascript is not a strong point of mine)

1: How do I use this in a PHTML page? I cant seem to include this library
2: How do I include this script in another JS file? Thus making "$.cookie" avaliable

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.