I want to redirect the user to a certain page if he/she has javascript disabled. I tried this code:
<noscript><?php url::redirect('controller/method'); ?></noscript>
// url::redirect is much like the location header
to no avail...
How do I do this?
Since the headers have already been sent, you'll need to use standard HTML markup:
<noscript>
<meta http-equiv="refresh" content="0;url=noscript.html">
</noscript>
Trying this on both Firefox and IE seems to work well... With JavaScript enabled, the <meta> tag is ignored. When it is disabled, the browser redirects to noscript.html.
There is no way to do a redirect based on if javascript is disabled. Why not do the opposite - redirect if javascript is enabled?
<script>
window.location = "...";
</script>
You can't use meta refresh tag then remove it using Javascript because the browser is set to redirect upon reading the meta refresh tag and it's too late for JS to manipulate it.
The only way is to either do what Daniel suggested, or to show up a link when there's no Javascript:
<noscript>
Click here to continue
</noscript>
Or you can try to fail gracefully: Do you plan for javascript being off?
Add this as the first element in your body, style it to suit, and perhaps offer a link inside of it to a noscript page:
<div onload="return false;">
<!-- PAGE CONTENTS -->
</div><noscript>JAVASCRIPT IS REQUIRED TO VIEW THIS PAGE</noscript>
I use a more verbose method of this (that basically makes it appear as a modal dialog and all pretty) on pages where a client does not allow me to use more compatable means. I've tested in IE6+ and all other major browsers with great success.
Related
I'm using the following code to use as a form of nulling referring script and it works perfectly but it just redirects them straight to the target URL.
How would I go about creating a 5 second delay so I can display some adverts for 5 seconds before redirecting them?
You can send php header with timeout refresh. http://php.net/manual/en/function.header.php
<?php
header( "refresh:5; url=wherever.php" );
?>
What about using sleep()?
function method1(...) {
sleep(5);
... rest of the code
Note however that it is more recommended to use Vahe Shadunts's answer, which uses header() instead.
The refresh header does the job but I'd like to highlight some potential issues:
It is not specified in the HTTP standard. Wikipedia says:
Proprietary and non-standard: a header extension introduced by Netscape and supported by most web browsers.
But it has been around for almost 20 years now and I don't know of any browser that does not support it (could not find a reference though)
Some browsers do not use the cache on a page redirected with refresh. It has been demonstrated for Internet Explorer here: http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/meta-refresh-causes-additional-http-requests.aspx and I coud reproduce it on Firefox. Chrome does not have this issue.
Alternative: JavaScript
You can add a JavaScript on the intermediate page, that opens a new page after X seconds. Add this at the bottom of the page to redirect to http://www.example.com/target after 5 seconds:
<script type="text/javascript">
window.setTimeout(function() {
window.location.href='http://www.example.com/target';
}, 5000);
</script>
Combination
As a bonus, you can fall back to the refresh header if JS is disabled, using the meta directive http-equiv that tells the browser to act as if a certain HTTP header has been sent. Because it is part of the HTML source, you can wrap it in a <noscript> element. Add this to your <head> additionally to the JavaScript above:
<noscript>
<meta http-equiv="refresh" content="5;url=http://www.example.com/target" />
</noscript>
Now, the page redirects with JavaScript if available for the best performance, and uses refresh otherwise.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect if JavaScript is disabled?
In my php application i need to check whether the javascript is turn on or not in browser. I have tried this <noscript><p>javascript is off<p></noscript> it's working fine.But i need to redirect to a page if javascript is OFF so developed like this
<noscript>
<?php header('Location: index.php');?>
</noscript>
But i/ts always redirecting to index.php, there is any way to do this.
your solution clearly cannot work, since php is executed before the page is served on client
you could instead do something like
<noscript>
<meta http-equiv="refresh" content="0;URL='http://example.com/'">
</noscript>
in the head of your document
<noscript><meta http-equiv="refresh" content="1;url=error.html"></noscript>
try this to redirect if JS is disabled
Php script runs independently of js
There are known issues with noscript
Instead do this:
<html>
<head>
<script>
window.location.href="javascriptEnabled.html";
/*Since javascript is enabled redirect to javascript based page*/
</script>
</head>
<body>
Showing Static Website since javascript is not enabled..
</body>
</html>
There is not a good way to do it, because without client side interactivity, you can not get information back from the client.
You could do something like this...
Set up a meta redirect to trigger after 5 seconds
use javascript to override the meta redirect
Therefore, if javasccript is enabled, you get to the javascript page, otherwise the non javascript page
It does redirect to index.php because the <?php header('Location: index.php');?> is processed by the server while the <noscript> is processed by the client ..
A correct way to do this is to set a cookie using javascript on the client, then check for the cookie's presence with php on the server, If the cookie does exist, then javascript is on, Else, it's off.
You could use a different approach here
Use the no javascript version as default version
Use a javascript redirect to the enhanced version
Remember it's better to not have separate versions of the site. You could progressive enhancement with libraries as http://modernizr.com/ or https://github.com/filamentgroup/enhance
is there way to reload a web-page, when javascript is disabled in the brwoser?
I mean, if the user visits the page first time, and has no javascript enabled, the page reloads with a get parameter like page.php?js=false.
Maybe, thanks for hints.
The only way you could force a page reload, without javascript, is by use of a meta refresh tag. However, this is the wrong way to go: better display that page for the javascript-deprived, and redirect those who DO have javascript to a more appropriate page instead.
<noscript>
<meta http-equiv="refresh" content="2;url=page.php?js=false" />
</noscript>
You can use <noscript> tag with refresh <meta> tag.
Adding to Sarfraz's post, you could do this, to match your request:
<noscript>
<meta http-equiv="refresh" content="2;url=page.php?js=false">
</noscript>
You can do it the other way around:
Assume javascript is not available, and
redirect using javascript to page.php?js=true.
I was using this code to reload the page, however I'd only tested it on Chrome and when I tried it on Firefox I realized it didn't work. How do I make it work in other browsers?
echo 'Reloading. <META HTTP-EQUIV="refresh" CONTENT="0">';
Thanks
Instead of pushing out invalid HTML, instead, send the actual header using PHP's header function.
header("Location: http://where.you/want/to/redirect.to");
If the page has already loaded (even partially), then this approach will not work -- in that case, either using JavaScript or the <meta> tag will do the trick -- just remember that the <meta> tag must be added to the <head> portion of your HTML file.
<script type="text/javascript">window.location.reload(true);</script>
What you are doing is telling the browser to refresh. However, the <meta> tag you are using is meant to be in the <head> part of the document. That is probably why Firefox isn't properly executing it.
If you instead use the blurb above, which is javascript, you can put that code virtually anywhere in the document and it will cause the page to refresh.
My guess is that it's not working because it's not in the <head> tag. Generate a properly formatted HTML document and it should work. But why in the world would you want to refresh the page after zero seconds?
i am trying to redirect users without javascript enabled to our help page (help.php), specifically the part that talks about enabling javascript (help.php#nojavascript).
however, my meta refresh is not working! it keeps on refreshing the same page! i tried redirecting to a different page with a .html extension instead, and that works, so why doesn't this work?
...
</script>
<!-- if user doesn't have JS enabled, take them to help page -->
<noscript>
<meta http-equiv="Refresh" content="3;url=help.php" />
</noscript>
</head>
...
Try to use an absolute path:
<META HTTP-EQUIV="Refresh" CONTENT="3;URL=http://www.some.org/some.html">
For one thing, try taking it out of the NOSCRIPT element. Browsers with scripting enabled are supposed to ignore everything inside NOSCRIPT.
i was including a file that redirected the user to home.php =( sorry!