MyBB Community Forums
How to detect that JavaScript and/or Cookies are disabled? - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Extensions (https://community.mybb.com/forum-201.html)
+--- Forum: Plugins (https://community.mybb.com/forum-73.html)
+---- Forum: Plugin Support (https://community.mybb.com/forum-72.html)
+---- Thread: How to detect that JavaScript and/or Cookies are disabled? (/thread-148695.html)



How to detect that JavaScript and/or Cookies are disabled? - Flavius Popa - 2013-12-06

Hello,

Is there any plugin for MyBB to detect if javascript and/or cookies are disabled, without refreshing the page ?

I tried to make one, it works for javascript but I'm not sure if is safe to create a hidden form and submit it with javascript and see if is enabled or not.

Also, is any method in MyBB to create a custom cookie?
I used '$_COOKIE["js"]' but again... I think there is a better way to do this.

<?php
// access denied
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}

// hook
$plugins->add_hook("pre_output_page", "checkjs");

// info
function checkjs_info()
{
	return array(
		"name"			=> "Check Javascript",
		"description"	=> "A sample plugin that check if javascript is disabled",
		"website"		=> "http://mybb.com",
		"author"		=> "",
		"authorsite"	=> "",
		"version"		=> "0.1",
		"guid" 			=> "",
		"compatibility" => "16*"
	);
}

// checkjs
function checkjs($page)
{
	if(isset($_COOKIE["js"]) && isset($_COOKIE["sid"]) && $_COOKIE["js"] == $_COOKIE["sid"])
	{
		return $page;
	}
	if(isset($_POST['jstest']))
	{
		if(isset($_COOKIE["sid"]))
		{
			setcookie("js", $_COOKIE["sid"]);
		}
		// JS is enabled
		$nojs = FALSE;
	}
	else
	{
		// create a hidden form (and submit it with javascript)
		echo '<form name="jsform" id="jsform" method="post" style="display:none">';
		echo '<input name="jstest" type="text" value="true" />';
		echo '<script language="javascript">';
		echo 'document.jsform.submit();';
		echo '</script>';
		echo '</form>';
		// JS is disabled
		$nojs = TRUE;
	}
	if($nojs)
	{
		$replace = '<p>Javascript is disabled!</p><div id="content" style="display:none">';
		$page = str_replace('<div id="content">', $replace, $page);
	}
	return $page;
}
?>

I appreciate any help. Rolleyes