我可以将Javascript条件的结果导入PHP变量吗?

Language : PHP & Javascript
I am maintaining a site in which I have two CSS, one for screen width less than 1024 and other for screen width greater than 1024. I use to match CSS by checking screen width in Javascript as follows.

var one=screen.width;
if(screen.width<1024)
document.getElementById('cs')='one.css';
else
document.getElementById('cs')='two.css';

Now I want the result of this if condition to be stored to a session variable in PHP. How can I implement this and is this really possible? Someone please help me out.

Thanks.

No. JavaScript is a client-side language, and PHP is a server-side language. By the time your JavaScript is parsed, PHP has been and gone.

Yes .You can make a ajax call with the data and set the session variable. I hope you aware about ajax.

first thing : you dont need php for your mission.It can be done using plain javascript.

Just give it a simple check:

var parameter_one = "http: //stackoverflow.com/one.css";  //your absolute path
var parameter_two = "http: //stackoverflow.com/two.css"

if (screen.width < 1024) {
    load_me(parameter_one)
} else {
    load_me(parameter_two)
}

function load_me(file) {
    var head = document.getElementsByTagName('head')[0];
    var link = document.createElement('link');
    link.rel = 'stylesheet';
    link.type = 'text/css';
    link.href = file;
    link.media = 'all';
    head.appendChild(link);
}

First store css file name in variable css.

   if(screen.width<1024){
        document.getElementById('cs').value ='one.css';
        var css = "one.css";
    }else{
        document.getElementById('cs').value ='two.css';
        var css = "two.css";
    }

After that You can do this in three ways:

1) Send css value to server through ajax request and set it in session.

2) Save css value in sessionStorage but it works only on html5

sessionStarage.setItem("css", css);

You can access this value onload of page by

sessionStorage.getItem("css");

3) Save css value in cookie in javascript

document.cookie = "css="+css+";";

Cookies are sent to server when you send request to server. There you can set it in session variable.

$_SERVER["css"] = $_COOKIE["css"];

I think this will be efficient way.