views:

203

answers:

4

I have a website, where I am trying to use Ajax to update some stuff on the page without reloading it. However, there is a good chance that many of my users will be using mobile browsers that don't support Javascript so I am trying to design the page with meta refresh tags, that somehow work only for users without Javascript. Is there any way to do this?

I tried putting the tag within a noscript element, but my primitive cell phone browser wouldn't acknowledge it. I am thinking of maybe setting a cookie to remember if the user's browser supports Javascript, or having one version of the page that works without Javascript, and tries to use Javascript to redirect the user to a more sophisticated version, but I am wondering if there is a more elegant way to do it. Does anyone have any ideas?

+4  A: 

Meta tags are awful in this case. What about search engines??

What you should do is to make is something like I've outlined here. Your links should point to full working sites as it were a web 2.0 page. Then with event handlers (onlcick) you enhance the user experience by using ajax.

So ajax users will not go to links, the link is rather processed when clicked and sent an ajax request to the exact same url but with an ajax GET parameter.

Now on the server side you have to be able to generate the whole site by some method. If it is an ajax request you send the related content. If it is not an ajax request, yo generate the full site with the related part embedded.

Your site will be SEO friendly, available to mobile users, and progressively enhanced for people on modern browsers and platforms. Finally ajax generated hash links will be usable, even as links.

Awesomeness. :)

galambalazs
How are meta tags awful? I am not trying to use them to redirect. I am just using them to refresh the page, as a fallback for users without Javascript support. I don't see how that would affect search engines.I am not completely sure if this is what I want to do, but I am still curious as to how I would do it.
mikez302
you couldn't figure out how you should do it, isn't it enough for you? :) Have you checked the link???
galambalazs
I checked the link. It seems kind of tricky to complicate the code on my server to serve the data in 2 ways. If I do it, I'll have to deal with some quirks. If I put an important part of the URL after the # sign, and someone bookmarks the page and tries to access it, it won't load as usual unless I have some JS to load the content. Same thing if the user hits the back button. I'm not sure if it would be good for search engines to make my URLs this way. They may see the # and think it is just links to different parts of the same page. I think it may be simpler with the meta refresh tag.
mikez302
you haven't read a bit of my post then :)) **The urls should point to full working sites not hashes.** **Event handlers** are attached to the links. They will make the ajax request and change the hash in the url. And if you've read it the full, you would know that the ajax generated **urls are working** when someone comes to them from the outside. And it is handled on the server side not on the client side. The example even demonstrates that. Please read it carefully next time...
galambalazs
+1  A: 

I agree that meta refresh is not the right way forward here. In addition to galambalazs' link, search on "progressive enhancement".

However, in the spirit of answering the question, you could try the following. It's untested, may not work in all browsers, but should be along the right lines:

var i, refAttr;
var metaTags = document.getElementsByTagName('meta');
for i in metaTags {
    if( (refAttr = metaTags[i].getAttribute("http-equiv")) && (refAttr == 'refresh') ) {
        metaTags[i].parentNode.removeChild(metaTags[i]);
    }
}

Whether removing it would stop the browser from refreshing in time remains to be seen.

Jhong
Meta tags cannot be overridden by JavaScript.
Hrishi
I tried that in Firefox (after putting the "i in metaTags" in parentheses). I got a Javascript error saying "metaTags[i].getAttribute". I guess it can't be done, at least not that way.
mikez302
you can't use a for(i in ...) loop here. the `in` operator iterates through the keys of an object. In this case you need to use a regular numeric loop: `for(i=metaTags.length-1; i>=0; i--) { ... }`You iterate backwards because a removeChild will change metaTags.length
bluesmoon
+4  A: 

You cannot override meta refresh tag with JavaScript.

However you can do this

Suppose your page is at ->

http://example.net/mike.html Put the following code there->

<script type="text/javascript">
window.location = 'http://example.net/mike/for_Those_With_JavaScript_Enabled.html';
</script>
Hrishi
I mean instead redirecting JavaScript-disabled browsers, you can redirect the JavaScript-enabled ones.
Hrishi
That's pretty clever. It's clean, simple, and solves the problem.
helixed
this messes with the back button though. a user clicking the back button with javascript enabled will get stuck in a redirect loop. they'd need to double click the back button to escape from it.
bluesmoon
+1  A: 

Just remove the meta tag with javascript:

<meta http-equiv="refresh" content="2;http://new-url/" id="meta-refresh">

<script type="text/javascript">
var mr = document.getElementById("meta-refresh");
mr.parentNode.removeChild(mr);
</script>

I've set the refresh timeout to 2 seconds above just as an example. You could probably get away with 1 second as well, but don't set it to 0 because the javascript won't get a chance to execute in that case. 0 is also annoying because it breaks back-button usability.

bluesmoon