开发者

Can certain PHP be served based on if JavaScript is enabled?

开发者 https://www.devze.com 2023-03-20 06:19 出处:网络
What I\'m trying to do is have one page that serves different markup based on if JS is enabled or not.

What I'm trying to do is have one page that serves different markup based on if JS is enabled or not. My idea is to have a base group of content served on the intial page load. There would also be some javascript function as the page is loading that submits an ajax开发者_运维技巧 call to server with post data letting the server know javascript is enabled. On completion of the ajax call, the page would refreshed/served again using the post data to know JS is enabled and serve the base content along with JS enabled content. My goal is to make this as seamless as possible.

*Edit: I should add I would like to maintain one page. So the page would just be refreshed with JS content. Not redirect to a new page.

My question is two fold, is this possible, if so, could I get some direction on the code I need to achieve this?


What you want to do is load the 'non javascript' markup inside a container, like <div id="container"> DEFAULT/FALLBACK MARKUP HERE </div>. Then, on document ready, make an ajax call to fetch the 'javascript enabled' markup and populate the #container div with it. It will only be populated if javascript is turned on :)

Example:

$(function(){

    $.ajax({
        url:'/javascript_enabled_html.php',
        dataType:'html',
        /* set your other params...*/
        success:function(data){
            $('#container').html(data);
        }
    });
});


You can't do it in a single step, but if you don't mind an intermediate page, you can have something like:

<noscript>
   <img src="js-is-disabled.php" />
</noscript>

and

js-is-disabled.php:

<?php
session_start();
$_SESSION['js_is_off'] = true;

This has the advantage of running on any browser which has JS disabled, as long as it's modern enough to recognize the <noscript> tag.

The alternative is to have a small snippet of JS fire off an AJAX request to call a script which flags JS as being available in the session. This might be a bit more reliable, since you'd only get this happening if JS is working properly.


Server-side scripts don't have access to this information, since it's not passed with the HTTP headers. An interesting solution I just found would be to have two pages. The first one has a javascript-fired redirect to the second page. So if they are hitting the second page, you know they have javascript enabled.

Another solution relies on non-valid HTML and may not work on all browsers. Use a <noscript> tag and put a meta refresh element inside it, like this:

<noscript>
   <meta http-equiv="refresh" content="0; url=http://example.com/nojavascript.php">
</noscript>

Then have another redirect that's fired onload in javascript that points to the javascript page. If it goes to the nojavascript.php they don't have js enabled, if it goes to the second, they do.

But I would just assume they don't have it turned on unless they get redirected by js, and build out the first page accordingly.


It can but it shouldn't. The only pages you should serve assuming JS is there are the ones called with Ajax.

And if you ever really need to know (which should normally never happen), you should assume JS is OFF until you get the information that it is ON.

0

精彩评论

暂无评论...
验证码 换一张
取 消