Jump to content

Recommended Posts

Hi there,

 

Sorry if this is the wrong forum to post this in, If it is please feel free to move it to the correct one.

 

I have a php page and I am trying to get a couple of lines to only be run if Javascript is disabled on the visitors browsers.

 

$returnpage = 'myphppage.php';
include('anotherphppage.php');

 

It is something probably simple but I can't see it...

I have tried:

<noscript>
<?php
$returnpage = 'myphppage.php';
include('anotherphppage.php');
?>
</noscript>

 

but that does not seem to work and I was hoping someone here would have an idea...

 

Many thanks in advance

 

Ross

Link to comment
https://forums.phpfreaks.com/topic/245957-piece-of-php-only-if-js-is-disabled/
Share on other sites

PHP cannot determine whether or not javascript is disabled... the PHP code will always execute.. however output of the PHP code will only be visible when js is disabled... use js to include your file instead..

 

You can't use JavaScript to determine what pages are include()ed in PHP! And, it is possible to determine if JavaScript is enabled on the user's browser.

 

All you need to do is simply have some JavaScript code when the user first access your site to set a cookie (e.g. 'javascript' = 1). Then in the PHP code you can check if that cookie is set. If so, then you know JS is enabled on the user's browser. Otherwise, you can assume it is not. Alternatively, you could implement an AJAX call that sets a variable for the user's session.

 

The drawback to both methods is that you need to have the page with the JS code loaded before any pages that need to react to whether the user has JS enabled or not. One solution would be to validate JS on the first page load when a users access your site. Here's a rough example:

 

if(!isset($_COOKIE['javascript']))
{
    //redirect to page to confirm JS or not
    header('http://www.mysite.com/jscheck.php');
    exit;
}

 

Then the page 'jscheck.php' would load and attempt to set the cookie with JS. Also, that page could utilize a META tag to refresh after 1-2 seconds. Upon refresh the PHP code could determine if the JS cookie was set or not. If not, it would set the cookie itself with a false value (i.e. 0) and then it would redirect the user (using the header() function) back to the original page requested.

 

So, the end result is that the very first time the users access the site, there may be a few seconds and a page refresh before they get redirected back to the original page. But then, after that, you can reference the cookie as needed.

Thanks for the replies.

 

I am trying to make my site seamless for those who may have javascript disabled, Unfortunately there are those who also don't accept cookies as much as I like the cookie solution.

 

Also yes I don't want to include a file using javascript I want to only include the file / set a piece of php if javascript is disabled which after searching and trying all day I am finding is not as simple as I imagined....

I am trying to make my site seamless for those who may have javascript disabled

 

Then you should have stated that from the beginning. If that is your goal then this is a needless exercise. It is a fairly strait-forward task to implement unobtrusive JavaScript so it is only used if the user has JS enabled. Let's take an example:

 

Let's say you have a paginated report of records. At the bottom of the page you have a select list for the user to "jump" to a selected page. Many such controls use JS to automatically do a submission when the value is changed so the user doesn't have to click a submit button. But, if the user doesn't have JS then you need a submit button (or you can remove the control completely and just let them use the other navigation links).

 

The solution: figure out how you want the page to behave when the user does not have JS enabled and build that FIRST. In this case we will give the user a submit button next to the page selection list. Pretty simple, just build a mini form with just that one select list (maybe a hidden field or two as needed) and a submit button. Once you have that working THEN you can implement some JS to enrich the user experience.

 

So, now - if the user has JS enabled - we want the form to submit as soon as the user changes the select list value and we don't want to display the submit button since it has no purpose. Simple. Create an onload even that will do two things: 1) Change the style property of the submit button so it is not displayed and 2) create an onchange trigger for the select list that will call a function to submit the form.

 

That is a very simplistic scenario, but it is the same process you should take for anything like this. Build the HTML only solution first, then add JS features on top of that and include code that will hide features/content that are not needed when JS is enabled.

SO PLACING JS IN HTML CAUSES ISSUES WITH CERTAIN BROWSER THEN..?

 

...what?

 

There's no need to have JavaScript code wrapped up in something like:

 

<script type="text/javascript">
<!--
   // JavaScript code
-->
</script>

 

It hasn't been necessary for years, since the early days of IE and Netscape Navigator.  It used to be necessary because the old browsers didn't know how to parse JavaScript, and instead merely spit the code out to the screen as text.

 

It doesn't cause a problem.  It's just completely unnecessary, and a sign that the coder doesn't know what they're doing.

 

Now, a commented out CDATA block:

 

<script type="text/javascript">
//<![CDATA[
   document.write("Hello World!");
//]]>
</script>

 

Is used to ensure that an XHTML or XML document will validate.  That said, who uses XHTML any more?

unfortuanately there are still people that use old browsers like IE6 < that will not have js enabled.. I have received complaints before on the websites that I have designed because of this..  I had a mindset like you until that time.. which is why I still use them. I guess it's more of a habit now then anything..

IMO, it's a bad habit.  Catering to the minority only forestalls the change they'll need to take in order to join the rest of us in the modern world of the web.  It's easier, and better, to have a noscript tag with links to modern browser alternatives they can use.

 

It's akin to wasting time/effort ensuring that a site looks good on 640x480 monitors.

For shits and giggles, the system requirements for the latest official versions of Firefox, Chrome, and Opera:

 

http://www.mozilla.org/en-US/firefox/6.0/system-requirements/

http://www.google.com/support/chrome/bin/answer.py?answer=95411

http://www.opera.com/browser/download/requirements/

 

I don't think requiring users to have Windows XP is too much to ask.

I don't, it's 2011! Why would they bother? Google are quite good in my opinion for moving technology forward.

i definitely agree with that, and in my opinion chrome is by far the best browser out of the lot..the reason why I found it interesting was because of opera and firefox still allowing win2000!!

i don't know the min requirements for IE atm.. they probably allow MS-DOS... heh

I am trying to make my site seamless for those who may have javascript disabled

 

Then you should have stated that from the beginning.

 

I did.....

I have a php page and I am trying to get a couple of lines to only be run if Javascript is disabled on the visitors browsers.

 

And I don't see how this is a needless exercise?

The rest of your post was not very helpful at all.

 

It is obviously not as straight-forward as you say and your example has nothing to do with what I am trying to do at all.

 

I am trying to not include a piece of PHP if javascript is disabled literally 2 lines of PHP and everything is built FIRST I am just trying to change one thing!

So you build a site and then down the line you think of something that could make the site run smoother but because you didn't think of it in the beginning as you already built it you think I'm not going back to change that regardless?

 

If that is your goal then this is a needless exercise. It is a fairly strait-forward task to implement unobtrusive JavaScript so it is only used if the user has JS enabled. Let's take an example:

 

Let's say you have a paginated report of records. At the bottom of the page you have a select list for the user to "jump" to a selected page. Many such controls use JS to automatically do a submission when the value is changed so the user doesn't have to click a submit button. But, if the user doesn't have JS then you need a submit button (or you can remove the control completely and just let them use the other navigation links).

 

The solution: figure out how you want the page to behave when the user does not have JS enabled and build that FIRST. In this case we will give the user a submit button next to the page selection list. Pretty simple, just build a mini form with just that one select list (maybe a hidden field or two as needed) and a submit button. Once you have that working THEN you can implement some JS to enrich the user experience.

 

So, now - if the user has JS enabled - we want the form to submit as soon as the user changes the select list value and we don't want to display the submit button since it has no purpose. Simple. Create an onload even that will do two things: 1) Change the style property of the submit button so it is not displayed and 2) create an onchange trigger for the select list that will call a function to submit the form.

 

That is a very simplistic scenario, but it is the same process you should take for anything like this. Build the HTML only solution first, then add JS features on top of that and include code that will hide features/content that are not needed when JS is enabled.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.