The “noscript” tag approach does not suit my purpose, as it will not allow me to make decisions in PHP.
What I am looking for is running a small piece of JavaScript that runs an AJAX PHP script that sets a session variable “jsDetected” to true.
When I get to displaying my page, if that session variable is not set, it would be fair to assume that JavaScript is not loaded, and the page will not be display. Just a warning message to turn JavaScript on.
The problem is to do this early enough in the code, even before the jQuery library is loaded.
I am looking for some quick and dirty XMLHttpRequest() call that just does a GET or POST and does not care about results from the call.
Something like this ?
xhttp.open("POST", "ajax_test.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
var request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
// Success!
var resp = this.responseText;
} else {
// Error :(
}
}
};
request.send();
request = null;
IE8 Compatible pulled from: http://youmightnotneedjquery.com/
1 Like
This works. Thanks all for your help!
<script>
function jsDetect()
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "jsDetect.ajax.php", true);
xhttp.onreadystatechange = function ()
{
if (this.readyState === 4)
{
if (this.status >= 200 && this.status < 400)
{
// Success!
var resp = this.responseText;
alert(resp);
}
else
{
// Error :(
alert('error');
}
}
};
xhttp.send();
xhttp = null; // destroy
console.log('jsDetect done');
}
jsDetect();
</script>
1 Like
AJAX file:
<?php
$_SESSION['js']['detected'] = '1';
echo 'OK'; // not needed
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.