I'm new to javascript and need to know how to store variables in cookies so that I can later use them later in php code.

Here is the basic javascript I want to use:

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

Now, I want to save myWidth and myHeight in a cookie(s) but I don't know how to. The window.alert() will only be for testing purposes and will not be needed in the final code. I also need to know if the codes need to be in certain sections such as <head>, etc. Do I need to change any lines of code or need to add anything.

Thanks in advance fo your help.

?

If you want to send out data as GET

just append '?width=xxx&height=xxx' to href and retrieve using $GET

So can I use it something like this: ?width=myWidth&height=myHeight
so that I can place variables in it? Or do I have to concatenate the value somehow, and if so, how do you do that in javascript?

Also, on another page I need to append a php variable to the href so does it work to mix and match the php & javascript variables? That is why I was thinking I would need to use cookies, but maybe it would work like this?

This is sort of how my href would look like:
website.com/page.php?id=&width=[javascript variable]&height=[javascript variable].

I'm guessing it would work to echo javascript codes to the client's computer so maybe this would work. I don't think I have ever done that before.[php variable]&width=[javascript variable]&height=[javascript variable].

I'm guessing it would work to echo javascript codes to the client's computer so maybe this would work. I don't think I have ever done that before.

website.com/page.php?id=&width=[javascript variable]&height=[javascript variable]

this is right - set up window.location to the above

in page.php u can retireve the variable as $_GET['id'] etc . about getting php variables as js variables its easy , just google if you dont get it :)[php variable]&width=[javascript variable]&height=[javascript variable]

this is right - set up window.location to the above

in page.php u can retireve the variable as $_GET etc . about getting php variables as js variables its easy , just google if you dont get it :)

I am wondering how to get the variables to the href though. If I send the javascript command page.php?width=myWidth, when I get to the other page will width then equal "myWidth" or will it equal the VALUE of myWidth.

I'm not wanting to get php variables as javascript, I am wanting the opposite. I know that isn't possible directly so that is why I am trying to work around it.

These javascript variable will either be read from the href into php or from cookies. I'm quite familiar with requesting php variables from the href, I also know how to post php variables to the href, now all I need to figure out is to post the javascript variables to the href and then possible the php/javascript variables to the href. I haven't had much luck searching for this information in a way that I can understand it but I guess I will keep trying.

page.php?width=200px -- its common thing it would have just missed you while commonly browsing thats all

now $_GET -> 200px

But the width/height won't be a known number such as 200px. My original javascript function gives me the current size of my browser and I want that variable (whatever it happens to be at the time) to be sent to the href (or cookie, whatever). So it is the variable, NOT a known 200px that I want sent. If it was a standard number that I knew I wouldn't need any of this other stuff including sending the value in an href.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.