Jump to content

Detect javascript and redirect based on result...


bluefrog

Recommended Posts

Hi all, I'm REALLY new to PHP and so I'm a bit stuck.

 

What I am trying to do is detect if a browser is using javascript and if so send it to one url and if not send to another url.

 

Any help?

 

The code I have been working on is:

 

<?php

ob_start();

if (isset($_POST['jstest'])) {
  $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>';
  // the variable below would be set only if the form wasn't submitted, hence JS is disabled
  $nojs = TRUE;
}

if ($nojs)

{
    header("Location: http://www.google.com");
}

else

{
    header("Location: http://www.ebay.com");
}

?>

 

but if I get the code to detect java on or java off then it won't redirect, if I get it to redirect it doesn't detect the javascript status. Tearing what I have left of my hair out !!!

first, JAVA and JavaScript are very different, so be careful when using those terms

 

second, your understanding of Client and Server side scripting is a little off. PHP is a server side language. When the user requests a page, all PHP code in the script is processed, end what ever is echoed/printed out, is sent to the user's browser. Then, the browser processes all the client side code (like HTML/CSS/JavaScript).

 

I can explain further, but I would like to recommend not doing this at all. JavaScript shouldn't be a requirement for your page to work. A working page should load, then JavaScript should be added to make things smoother and more user friendly. By adding a JavaScript test of this nature, the user will see a page then be redirected to another...which is confusing to the user. Then, if they try to use the back button, it will keep sending them forward again...which is annoying to the user. Finally, when search engines try to crawl your site for information, they won't know to follow your javascript redirect, meaning they won't find all your content...which is bad for you.

Thanks for the reply. You're right of course I have very little idea what I am doing with this lol.

 

OK, here is what I was aiming to achieve:

 

Take the user and based on their browser abilities move them to the correct page.

 

i.e. I want Opera and Google Chrome to have a different page.

 

Everything else can be to the normal page. Also I wanted to add a geo IP filter for UK and USA/Canada moving; UK to 1 page, USA/Canada to another and the rest to another.

 

My background is in marketing and this is for a specific project I'm working on which won't have any impact on the visitor as they know they are being redirected.

 

So the javascript element isn't an important part, just the only way I could think of to filter the browsers out (typically not java enabled by default).

 

So with that in mind, do you have a better idea to shunt these users with opera and chrome to one page while continuing to filter the rest for IP location?

Archived

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

×
×
  • 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.