Jump to content

Making Ajax-website Compatibel


Redroest

Recommended Posts

Ok I was dealing with the following problem all day long:I succesfully made my adminpanel and it works in both non-javascript browsers and javascript-enabled browsers. I also use <noscript> to hide parts of text and javascript coding that are only useable to javascript-enabled users.Soon I started to realise that <noscript> wasn't enough to hide javascript functions so I started using a javascript function that switches between display:none; and display:block; using DOM:<div id="javascriptEnabled">Een stuk tekst</div><div id="javascriptDisabled">Een stuk tekst</div>It seem to work out for me when I stumbled on the next problem:My Serversided php code makes everything in functions. When a function is just for processing, no output will be given, but when it is a function that returns output I have to include a header.php and a footer.php

function withoutput(){  include("header.php");    echo "This is a testpage";  include("footer.php");}

I have made my entire website with this layout and it is similar to the phpnuke philosophy. I call every page with a switch:

switch($task) {  case "withoutput":	withoutput();	break;			default:	  index();}

No the problem:When I want to do the same using AJAX it still works. I just send the output of the function to a <div="MainPage"></div> using innerHTML. Now in this case include("header.php"); and include("footer.php"); are not needed since there will be multiple headers in that case.I tryed making an AJAX code that checks for enabled javascript sends back the results to a php script on a server witch places a cookie that says: javascript=1 (or javascript is enabled) but every time the page still has to be refreshed in order for the cookie to become active.Can anone please help me with this?

Link to comment
Share on other sites

Cookies probably aren't the best idea. One way would be to store that in the session instead, a more elegant solution would probably be to send a custom header with all of the ajax calls and then check for the header in PHP to see if the request was sent using ajax.

Link to comment
Share on other sites

The session also need a page refresh, just like I tried with a cookieCan you give me a little example of what u mean with: "send a custom header with all of the ajax calls and then check for the header in PHP to see if the request was sent using ajax."?

Link to comment
Share on other sites

The session also need a page refresh, just like I tried with a cookie
Not if the session is already started. If you're starting the session in the ajax response then that won't work, it needs to already be started.
Can you give me a little example of what u mean
Look into the setRequestHeader method of the XHR object, you can use that to add any header you want. In PHP you can get the headers from the $_SERVER array. You can use print_r to look at the contents of it:print_r($_SERVER);e.g.:xhr.setRequestHeader('X-AJAX', 'True');...if (isset($_SERVER['HTTP_X_AJAX']))
Link to comment
Share on other sites

Ok, I have been working on and searching for this all day long and made the conclusion that there is nothing to find about sending to the server if the client has javascript disabled so I had to be creative. I think that making an AJAX version to check if javascript is disabled/enabled is really un-necessary, because the script will not work in the first place when javascript is disabled and I still need the page to refresh to make a session work. I decided making php to check every page reload if there is a session variable called Cmode. (stands for compatible mode). When there is no such session-variable the page will conclude that javascript is enabled. I think it will be best to just show my script what happens when the user has javascript disabled:

echo "<head>";echo "<script type='text/javascript' src='system/XMLHttpRequest.js'></script>";//This php script is activated only when the meta-refresh is activated. //The Cmode session variable will be activated when javascript is disabledif(isset($_GET['CheckJS'])){  $_SESSION['Cmode'] = 1;}//When the meta-redirect is activated the page must return to the page of origine$filename = basename($_SERVER['PHP_SELF']);$ChopURL = explode($filename, $_SERVER['REQUEST_URI']);//if no basename is given in the url, it will trow an offset error so we first have to find if the basename is in the $_SERVER['REQUEST_URI'].if(strpos($_SERVER['REQUEST_URI'], $filename)) {  $RedirURL = $filename.$ChopURL[1];} else {  $RedirURL = $filename;}if(empty($_SESSION['Cmode'])) //If the session Cmode is empty{  //The meta-tag is between the <noscript></noscript> tags, this means that the page will only be refreshed when javascript is disabled  echo "<noscript>";   echo "<meta http-equiv='refresh' content='0;url=".$RedirURL."?&CheckJS=true'>";   echo "</noscript>";}echo "<noscript><style> #jsonly { display: none } </style></noscript>"; //Opposite of <noscript> will be <div id='jsonly'> witch hides tags used by javascript when javascript is disabled.

As you see, the page will redirect when javascript is disabled, a session variable (Cmode) will be activated witch stops the loop. Nothing will happen when javascript is working. Now php only has to check everytime if the Cmode session has been activated to know whether it can activate javascript.I also use the <noscript> tag to hide any tags used only when javascript/AJAX is disabled. The same counts for the <div id='jsonly'> to hide the stuff that is only used by disabled-javascript browsers. <div id='jsonly'> is double. I just made it as a quick way to hide javascript-only tags. (makes my phpcode clear)I have just one question about my script: Will this give problems for Google bots that try to index the pages?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...