Unable to reload HTML5 file from server in IE and Firefox - php

I have an HTML5 page that displays not much more than just a list with names a simple form in which the user can fill out their name. The submit button calls as action a PHP file that adds/removes the name to/from the list, and then redirects (by use of header()) back to the HTML file that called the PHP file.
In Google Chrome, after the user hits the submit button he immediately sees that is name was properly added/removed to/from the list with names.
In IE, Firefox (and according to someone who tested for me also on Android and in Safari), after hitting the submit button, the previous version of the html file are shown, without the changes that have just been made to it by the code in the PHP file. Even after reloading and even after clearing the cache, which I tried in IE and Firefox, still the old version is shown. While at the same time, loading the file in Chrome does show the altered (=current) version. Only after maybe 20 minutes or so, it is finally possible to load the updated html file in IE and FF.
Even reloading with JS window.location.reload(true);, just to see if that at least forces the updated version, does not update the page (it does reload, but still shows the unaltered version).
I searched the internet for about 6 hours, but found no solution, or even anyone else reporting this problem. Adding META HTTP-EQUIV ... is not valid with html5, and indeed (eventually tried it anyway) does not solve anything. I am not using a cache manifest (never used it). So the page should not be cached, but as I mentioned, even clearing the cache and reloading does not give the new version of the page (not even after restarting IE/FF).
I am still learning, only started designing web pages last year. So, I (clearly) don't exactly know all that is going on between the server and the browser. So this problem seems really odd to me, and I am very eager to find out what is going on here. Also, it is vital for this page to download and display the current version from the server, because users need to see that their name is added/removed to/from the list.
Here is the outline (sorry, probably not the right English word) of the html page:
<!DOCTYPE html>
<html>
<head>
<title>Title text</title>
<link rel='stylesheet' type='text/css' href='corStylesheet.css' />
<meta name='robots' content='noindex,nofollow' />
</head>
<body>
[content here]
<script src='corJavaScript.js'> // (some of the js did not work when place in head)
</script>
</body>
</html>
So, I basically have two questions here:
1. What is going on here? i.e. how is it this impossible to load the current version of the html file?
2. What can I do to force the current version to be downloaded from the server and shown?

If you wish to force the browser to reload the page every time you can set the header so that no caching of the file is allowed.
Either you can do it via PHP (read more about it here);
header("Cache-Control: no-cache, must-revalidate");
Or you can set it directly in HTML:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
EDIT: Just found an extremely good post about how to set this in different languages; How to control web page caching, across all browsers?

In your redirect in the PHP side you can add some number unix time or micro unix time
to make the browser think it is new page:
header('location: yourhtmlfile.html?'.microtime());

My (Javascript) approach is, appending a random string to the requested URL.
The resource is then considered a new resource, bypassing the browsers cache.
This also works when you want to reload an external resource from an address where you can't add/modify response headers (like on a 3rd party CDN).
var currentUrl = document.URL;
if (currentUrl.search('?') === -1) {
window.location.href = currentUrl + '?' + Math.floor(Math.random() * 11);
} else {
window.location.href = currentUrl + '&' + Math.floor(Math.random() * 11);
}

Related

send 404 File not found header, and then provide alternative file for download

I have a file which contains a version number in the filename and is updated almost every month.(The version number is necessary for other software which needs that to work properly.)
To make published links with an old version number also workable, for user convenience, I wrote a php script which does a redirect with a 301 header, and provides the latest file with the current version number for download.
Now I found in the log file that old links with an old version number a kept in the index of search engines, and it's more and more growing that they aks for old files(urls), and even with the 301 redirect they do not update the stored links and ask for the old files over and over again.
After reading a while I found that the only way to get them out of the index is to send a 404 or 410 header, but it seems on the other hand, that this doesn't let me send a file with that header.
So the question is do you see any chance how I can send a 404 or 410 header to get old links away from search engine indexes, and at the same time provide the latest file for download to have old links still working for user comfort?
You can use a combination of the meta http-eqiv attribute and JavaScript to allow requests from browsers to complete. This method will not work for wget, curl or other such automated programs.
<!doctype html>
<html>
<head>
<title>File not found</title>
<meta charset="utf-8"/>
<meta http-equiv="refresh" content="5;URL=http://somedomain.tld/file_v1.txt"/>
</head>
<body>
<p>The requested resource does not exist. However, an older version of the file was found and will begin downloading in <span id="countdown">5</span> seconds.</p>
<p>Please click here if the download does not begin or you no longer wish to wait.</p>
<script>
// you can get #countdown and using setInterval decrease the seconds to enhance the user experience
// additionally, you can use `window.location.href = "http://somedomain.tld/file_v1.txt";` at 0 seconds
// incase the user has meta redirects disabled
</script>
</body>
</html>

want to stop load the script for IE in a particular page by code

I created a website in that every page is working fine in every browser but ONLY one page(form Page) in IE is taking too much time for loading and the error I get is that IE is running a long script and is not responding.
I found a solution for this that in IE under the tool section I have to disable the script option and it works also but its not possible to tell every user to follow this solution, so I wanted to know is there any script or code with the help of which I can disable the script ONLY For this FORM PAGE in IE only for every user dynamically(I created a separate template for this form)
Thanks in advance.......
How about following..
<script>
if( /* Browser checks */ )
document.write( '<script src="scripts.js"></script>' );
</script>
If all your scripts are in one place, or at least not embedded all over the page, perhaps you should include them with JavaScript after checking the browser:
This is one scenario which illustrates why keeping your code http://en.wikipedia.org/wiki/Don%27t_repeat_yourself and all in one place is important.
For detecting browsers, check out http://www.quirksmode.org/js/detect.html, but note the link to their Object detection page instead if that's more useful for your use case. http://yepnopejs.com/ is useful for the latter, which is part of Modernizr.
Credits: Disabling JS from within Website for specific browsers
Hope it helps.
Try
http://www.quirksmode.org/js/detect.html
or
alert(navigator.appName);
Use IE conditional comments:
<![if !IE]>
<script>
// your non IE script here.
</script>
<![endif]>
For more info see: http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx
Additional explanation:
A note on how conditional comments work.
Conditional comments is an IE specific feature to expose or hide portions of content (not necessarily script) depending on IE versions or if the browser is non IE. For example:
<![if !IE]>
Hello non IE
<![endif]>
Will print Hello non IE on non IE browsers and nothing at all on IE. It is only an IF statement. It is not a magic way to erase content. For example:
Hello non IE
<![if !IE]>
Hello non IE
<![endif]>
will print:
Hello non IE
on IE and:
Hello non IE
Hello non IE
on non IE browsers. It does not delete the previous text. It merely hides the text inside it from IE.
As such, to exclude a script form IE you need to do this:
<![if !IE]>
<script src="non_ie_compatible_script.js">
<![endif]>
NOT this:
<script src="non_ie_compatible_script.js">
<![if !IE]>
<script src="non_ie_compatible_script.js">
<![endif]>
If you have no access to the original line that includes the script then there is nothing you can do.
If you have access to the script then you can use the other solutions posted on this page. But as you mentioned in your OP you don't have access to the script either so none of the solutions here will work. In fact, unless you have access to the server itself or some proxy that sits in front of the server (in which case you can intercept the entire page and delete the script inclusion tag before it's delivered to the browser) there is no solution to your problem.
Your best bet may in fact not be a coding solution but a human solution: talk to the owner/maintainer of the script or the owner/maintainer of your website and ask them to provide you with a way to turn off the script on specific pages.
Or do what #karthick suggests and debug your code instead of trying to disable the third party script.

PHP - Delay on redirect

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.

Send IE 7 (and below) to old/sub directory of site. <!-- [if IE 7]><"LINK TO OLD SITE"/><![endif]-->

I have just launched a site (using Joomla and a custom template), which doesn't display that well in IE7 (and I guess below too). I have looked around and have found out that you can link to different style sheets from my index.php, however, instead of linking to a different style sheet, I want it to link to the older site which is still live (under www.mydomain.com/old).
Is that at all possible?
As stated in the title, I have looked around and found out that you could use an if statement like this -
<!-- [if lte IE 7]><"LINK TO OLD SITE"/><![endif]-->
is what I'm trying even possible? I haven't got anywhere with it so far, trying the usual html tags of href="http://www.mydomain.com/old"
Any help would be great on this. I'm just getting stuck at the moment!
Conditional comments are used in the client-side part of your page, and so are not useful for PHP. You can use a conditional comment with JavaScript like this:
<!— [if lte IE 7]>
<script type="text/javascript">
top.location.href = "http://www.mydomain.com/old";
</script>
<![endif]-->
The disadvantage of this is that you are performing this task on the client machine, which is slower than if you performed the redirect on the server and sent to user to a different page instead. You can do this using PHP by checking the browser version and redirecting with header:
$browser = get_browser();
if($browser->browser == 'IE' && $browser->majorver <= 7) {
header('Location: http://www.mydomain.com/old');
}
Bear in mind that for this to work you must call header before any data is sent to the client.
Well, much reasonable would be to catch IE7 users before they started to render the page.
So it could be done with server-side script either with some mod_rewrite. Would be easier and faster.
I used Google to find this.
The objective of this technique is to enable redirects on the client side without confusing the user.
...
In HTML and XHTML, one can use the meta element with the value of the http-equiv attribute set to "Refresh" and the value of the content attribute set to "0" (meaning zero seconds), followed by the URI that the browser should request.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Tudors</title>
<meta http-equiv="refresh" content="0;URL='http://thetudors.example.com/'" />
</head>
<body>
<p>This page has moved to a <a href="http://thetudors.example.com/">
theTudors.example.com</a>.</p>
</body>
</html>

PHP / JavaScript. Browser "loads" forever, even after all resources are finished downloading, plus Opera/Chrome Issues

I have a page that does the following:
The browser loads a very simple page with a valid head and body, with only a script/noscript pair as content.
In the body, it has a script (script a) that runs a function onLoad. This function dynamically includes a second script (script b), and runs a function in it when it becomes available.
The second script is a .js file that does various work.
Both scripts are parsed by PHP and use the application/x-javascript content type.
Now, I have this all working just fine, except a couple of JS hiccups. JavaScript isn't one of my strong languages, so I'm hoping these are simple issues and somebody can point me in the right direction.
Problem 1: If I do a simple alert('you are in script b'); in the second script, it works as expected. However, if I do anything else, it works fine, and then the browser keeps indicating that it is loading forever. This is the color tween in firefox, or the spinning thing in IE.
I've tried ending the script in different ways, and nothing seems to help. Any idea how to indicate to the browser that the script is all the way loaded? It's a .js file that is forced to parse through PHP.
Problem 2: The second script doesn't seem to be included at all in either Opera or Google Chrome. Works fine in FF/IE, other than the loading issue. Can anyone see if Im using something that isn't compatible in the loading of the second script?
Thanks!
Update:
Thanks for the answers. I actually have firebug, which is why I know everything is working properly (in FF, at least). I don't actually know that the script isn't working in Opera/Chrome, but nothing happens.
It is quite a bit of code =o) I will copy the actual responses out of firebug and post those, so you can see exactly what the code is. As far as the webserver closing the connection, I was thinking that too, but it seems odd that if I make script b into alert('whatever'); it will alert and then stop loading, but it I do everything exactly identical, but make the script document.write('whatever); it will load forever.
Here are the scripts, updated, copied directly from the net tab of firebug:
Note that discoverfire.net is an internal domain, so you won't be able to load anything from there...
Initial HTML page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Welcome!</title>
<style>body { font-family:arial; }</style>
<script language="JavaScript" type="text/javascript" src="http://www.discoverfire.net/analytics/l/a.js">
</script>
<script language="JavaScript" type="text/javascript">
document.onload = Start();
function Start(){
TAFKing_version = '1.0';
TAFKing_lkey = '19-8O-KKA8HV';
TAFKing_Lander();
}
</script>
</head>
<body>
<noscript>
Oops! We can't forward you properly because your JavaScript is turned off.<br /><br />
<a href='http://www.discoverfire.net/analytics/l/noscript/19-8O-KKA8HV.html'>Please click here to continue.</a>
<img src='http://www.discoverfire.net/analytics/l/imp/19-8O-KKA8HV.png' border='0' alt='tell a friend' />
</noscript>
</body>
</html>
** Script A (...a.js): http://www.discoverfire.net/analytics/l/a.js **
function TAFKing_Lander(){
version = TAFKing_version;
lkey = TAFKing_lkey;
var scrb = document.createElement('script');
scrb.type = 'text/javascript';
scrb.src = 'http://www.discoverfire.net/analytics/l/b.js?lkey='+lkey+'&version='+version+'&cb=4eohe8e65'
;
document.getElementsByTagName('head')[0].appendChild(scrb);
Interval = setInterval("Waiter()", 10);
return;
}
function Waiter(){
if(window.TAFKing_LanderB) {
clearInterval(Interval);
TAFKing_LanderB();
}
}
Script B (...b.js): http://www.discoverfire.net/analytics/l/b.js?lkey=19-8O-KKA8HV&version=1.0&cb=4eohe8e65
function TAFKing_LanderB(){
document.write("there are just a whole bunch of doc.writes here that build a simple table");
}
I bet it is nothing related with the scripts, but with the webserver. Your description, specially that it affects many browsers, and some of them don't even run the scripts, leads me to believe that the webserver is not closing the connection. Perhaps the webserver is not properly handling HTTP/1.1 Keep-alive requests.
Try using Firebug in Firefox. Install it, enable it for your page, reload the page and check the "Net" tab for what really is keeping the connection open.
This is a lot of code to go through. You should definitely get Firebug to help you diagnose it. The latest version will even show you when/if the onload events occur.
Firebug will also allow you to output message simply by writing console.log('somevar=',var); to test their values. You can even use the console to test value after the page has loaded since you're using the global name space.
Off the top of my head, I would make sure the connection properly closes in php. Also
document.onload = Start();
would assign the result of Start() to onload, not Start which is defined later.
Also window.onload is more compatible/standard.
You may want to save the output of your js files as outputphpA.js and outputphpB.js, directly source those and see if the loading behavior differs. That should help diagnose if it's a php issue.

Categories