Welcome Guest, Not a member yet? Register   Sign In
browser js detect
#11

[eluser]mr_coffee[/eluser]
I can't recall who originally posted this, but this part needs to be added to ci/libraries/Input.php:

Code:
function isAjax() {
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest");
    }

Place before the last closing bracket.

In the controller, use something like this:
Code:
if ($this->input->isAjax()){
$javascript_enabled = true;
}

Somewhere... and then have javascript load a blank, flat view anywhere on the first page load. What I did for mine was use:

Code:
if (!$this->input->isAjax()){ ...

Before loading ANY view like the header files, footer files... this way I could load only the contents of a view whenever needed. My controller is organized to "switch" for every page asked for (I only write new controllers if there's a form). One case is: if the requested page is "checkjs" or some arbitrary name I wouldn't use elsewhere... which would load a session variable:

Code:
case 'checkjs' :
                $this->session->set_userdata('jsenabled','true');
                $data['page'] = 'checkjs';
            break;

This has the downside of only working if cookies are also enabled. "checkjs.php" is an empty view. I have Javascript make an ajax request on the first page load. I'm not sure if it's because I'm setting the session variable through the controller and not using JavaScript to set it... but the cookie value isn't available until the NEXT page is loaded, and so is practically worthless on the page the visitor lands on.

It's not critical. The reason I desired the check to begin with was for a MooTools slideshow I was using... which works with or without JavaScript. But it worked BECAUSE the images are all "visible" by default. Thing is, if you have JavaScript, there's no reason to make the pictures "visible" on the initial page load: the result would be that the images would quickly flash into the same div as they loaded... and THEN JavaScript would hide them all... and then proceed to display the slideshow as normal. If I hide them at the start, it goes straight to the normal slideshow display without the flashing of each image first... but someone without Javascript would never see an image in that case (since JavaScript is needed to "show" them again). I used the checkjs thing to set a cookie so that I could have that page showing images by default for people without JS... making for an ugly but functional page load.

So if the session variable is set, leave things alone... if it's not set, then set all image styles to "visibility:visible" or whatever as an inline style.

It works, but there is a page delay. I never did figure out why, but it's likely due to setting the session variable in the controller instead of using JavaScript to set the cookie. In fact, if you're going to use JavaScript to do it, you don't need the Ajax load either. This should work just as well:

Code:
window.onload('domready',function(){
  Cookie.write('jsenabled','true'); // or any value at all.
});

And then in any given page view:
Code:
if ($_COOKIE('jsenabled')) {
//something
} else {
//something else
}

But even then... cookies aren't available until the NEXT page load. So someone landing directly on my slideshow page (if they found it on a search engine or something) would get the uglified non-javascript version unless they clicked another page and came back to it.

Either of these methods are otherwise affective. Issue is that you're technically also counting on cookies ALSO being enabled. I've been completely unsuccessful in setting a value of any sort of globabl php variable to accomplish the same thing... even on an Ajax load, as technically that variable gets rewritten when the controller is called again for the Ajax load. All documentation points to: use a cookie if you need a value for a more than a minute... but then there's a page delay. Cookies are set AFTER the page has fully loaded, and can't be used prior to that.

I've done something very similar in a Joomla setup. That framework - as convoluted as it is - seems to count multiple page loads during a single page load. I set a number of cookies during the initial page landing, and had JavaScript manipulate the links of the page accordingly... on the same page landing. It's POSSIBLE that it works because the ajax load was in a different controller than the controller loading the main page. I've not experimented further on the CodeIgniter site to see if I made a second controller just to deal with the Ajax and cookie setting to see if it would work, but it might. On the other hand, might need a third or fourth controller (as I'm pretty sure that's how Joomla does its thing).

I've also no idea how to check for ONLY Javascript, even now... since the only way to have a lingering value - that I can tell - is to use a cookie. Plugins like CookieSave and NoScript will break things. Fortunately, those using the evil browser IE - which breaks everything else - won't impact logic like this since they won't know how to block cookies or JavaScript, and the pages will load as expected, if not improperly formatted, haha.




Theme © iAndrew 2016 - Forum software by © MyBB