I should detect the browser supports Javascript or not in laravel 5. As far as I know I have to use . But I am not sure which is the first php file which I can use in laravel 5?
Should I include it in public/index.php? It looks not correct. Looking forward your help.
I should detect the browser supports Javascript or not in laravel 5. As far as I know I have to use . But I am not sure which is the first php file which I can use in laravel 5?
Should I include it in public/index.php? It looks not correct. Looking forward your help.
Share Improve this question asked Sep 19, 2015 at 17:45 Georgi KovachevGeorgi Kovachev 6432 gold badges10 silver badges25 bronze badges3 Answers
Reset to default 5You cannot do it with PHP. You can use the <noscript>
tag in HTML.
Here is how you can do it:
<noscript>
This page needs JavaScript activated to work.
</noscript>
If JS is not enabled, that code block in the HTML document will be executed. You can place that in your blade templates.
<noscript>
tag in w3schools.
While you cannot rely on PHP by itself to detect whether Javascript is enabled, you can use Javascript to help to detect if Javascript is enabled.
Set a Cookie with Javascript
Javascript can set cookies. Thus, if the user does not have the cookie, then it is a good indication that Javascript was not enabled. If the user does have the cookie, then the user probably have Javascript enabled. (Although you cannot be 100% sure.)
<script>
window.onload = function() {
document.cookie = "hasjs=true";
}
</script>
Now you can use PHP to check whether the cookie exists.
if ($_COOKIE['hasjs'] == 'true') {
// js enabled.
}
Make an GET Request with Javascript
Javascript can also make get requests. You can make a get request to a "hidden" php script. If the script never receives the get, then it is a good indication that Javascript was not enabled. If the script receives a get, then the user probably have Javascript enabled. (But again, you cannot be 100% sure.)
<script>
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "/detectjs", true);
xmlHttp.send(null);
</script>
Once you receive the GET, you can note down that this user have javascript enabled for future references.
This may be a duplicate of other questions regarding javascript detection you may find your answer here. From a google search I found something on github that may be of some use link. It seems to support checking for javascript support. It may be using the same technique found in the stackoverflow link I showed you.
The manual way can be done by redirecting to a cookie pickup like so
index.php
<?php
if(isset($_COOKIE["javascript_checked"]))
{
$GLOBALS["javascript_enabled"] = isset($_COOKIE["javascript_enabled"]) ? true : false;
}
else
{
setcookie("javascript_check_original_request", "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], time()+3600);
header("Location: http://" . $_SERVER["HTTP_HOST"] . "/detectjs.php.php");
exit;
}
var_dump($GLOBALS["javascript_enabled"]); // prints out the result
detectjs.php
<?php
setcookie("javascript_checked", "true", time()+3600);
?>
<meta http-equiv="refresh" content="1;url=<?php echo $_COOKIE["javascript_check_original_request"]; ?>" />
<script type="text/javascript">
/**
* A function to simplify the process of creating a cookie
* @param string name
* @param string value
* @param int days
*/
function createCookie(name,value,days) {
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
createCookie("javascript_enabled", "true", 1); // creates a cookie that says javascript is enabled for 1 day
</script>
This will check for a cookie to see if the cookie pickup has happened, if it hasn't then it will redirect to a page that sets the cookie that the cookie pickup has happened and if javascript is enabled then the javascript will create a cookie that states it is enabled. it will then redirect back to the original request so that the user wouldn't notice a difference besides a second delay that it takes for the detectjs script to redirect back to the original request.
now that the check has been done, php can store a boolean into the $GLOBALS variable that contains a boolean for if javascript is enabled or not. You can reference this in any php file in laravel to make decisions if javascript is enabled or not using
if($GLOBALS["javascript_enabled"])
{
}
else
{
}
This can easily be done manually with the process above but if you want to use that laravel library I linked, more power to you.