admin管理员组

文章数量:1404923

I have just been altered to the fact that a user of my website is using a very old browser which does not run jquery (in this case Safari 1.x) and as a result can not access the login panel which uses jquery's slideToggle function.

Can anyone think of a fix which detects whether a browser is able to use jquery - and if not make that link go to a different page rather than showing the login panel?

I have just been altered to the fact that a user of my website is using a very old browser which does not run jquery (in this case Safari 1.x) and as a result can not access the login panel which uses jquery's slideToggle function.

Can anyone think of a fix which detects whether a browser is able to use jquery - and if not make that link go to a different page rather than showing the login panel?

Share Improve this question edited Oct 31, 2012 at 16:31 jam 3,6885 gold badges35 silver badges50 bronze badges asked Jan 21, 2011 at 12:05 TomTom 13k50 gold badges153 silver badges247 bronze badges 3
  • Does it work with no script on at all or is it that it does enough javascript to prevent access to the login panel? – Chris Commented Jan 21, 2011 at 12:09
  • I see what you mean, but that won't work because the user has javascript enabled, so the browser will ignore the noscript stuff – Tom Commented Jan 21, 2011 at 12:10
  • Would using an alternative library be acceptable? – Knu Commented Jan 26, 2011 at 18:22
Add a ment  | 

8 Answers 8

Reset to default 5

You could a little conditional check like

if(!'jQuery' in window) {
    // jQuery is not available
}

or, if Safari 1.x doesn't know about the IN operator (I'm not sure) use

if(!window.jQuery) {
}

I think there are alternative answers to this, but for me, I would have to weigh up the time it will take you to support his obsolete browser (I'm sure there may be other things inside the site), versus the payback to you...

In the plain HTML source code for the the href= of the login link, set that to a plain HTML login page.

Using jQuery, attach the click handler to the link, if this part fails, thats ok, the browser will just follow the href in the link to the plain login page, allowing your old-browser-user to login still.

$(document).ready(function(){
  $('#login_link_id').click(function(){
    // Your code here
  });
});

If you use javascript/jQuery you should ALWAYS ensure your site works perfectly without it. In this case if you have a login popup box; you probably assign a click event assigned after the DOM has loaded.

What you should do is ensure that if jQuery isn't present the link loads a "normal" login webpage as opposed to the popupbox. I use something similar to this:

<a href="/login.html" id="loginlink">Log in</a>
<script>
    if(!'jQuery' in window) {
        $(document).ready(function(){
            //assign on click event to loginlink
        });
    }
</script>

If jQuery doesn't exist then login.html will be opened normally.

Wow, seriously?! Safari 1.x?? Anyhow, try this...

var isJQSupported = false;

$(function() { //shorthand for document.ready

  isJQSupported = true;

  //your usual code


});

if (!isJQSupported) {

  window.location = "http://www.apple./safari/download/";

}

To me it sounds like safari 1.X has problems with jQuery internally. Which means simple checks like whether $ exists in the global space or whether $(function) does anything are not going to help.

The most likely root cause will be that javascript throws an error in loading of jQuery itself which will then stop the rest of your javascript code from execution.

There are four viable options here.

  • Either make the website work with noscript. Replace your login control with pure HTML and postbacks and ask the user to turn javascript off. This option is useful since you won't be fixing the issue for safari 1.x problems specifically.
  • You can make javascript check for safari 1.X and other non-supported browsers and only load jQuery through script tag injection or ajax if your user is using a supported browser. If the user is using a browser not patible with jQuery then you can instead use plain javascript.
  • Get a copy of safari 1.x and see why jQuery breaks. Then fix it and ask for it to pulled into the release of jQuery 1.5. This relies on the fix being something that does can be done without hacking and that the jQuery team agrees is worth adding in.
  • Ask the user to use a pliant browser.

There might be some more options. I would personally lean towards asking the user to use a pliant browser because supporting Safari 1.x is ridiculous.

This seems like a case where progressive enhancement is needed.

You have to do multiple checks

  1. see if $ exists
  2. see if $.fn exists
  3. [not sure if needed] check if $.support is a function
  4. check for feature support as needed with $.support() http://api.jquery./jQuery.support/

At the end of the check, when jQuery reports that features you need are present - the rest of the script can run.

If you're not sure which features mentioned in the support you use, then this might need a single test on Safari 1.x to see what are the values returned by $.support(), but that is what your nasty old-browser-user can do for you (if you prepare code and publish) and report the resulting text. Then you pare the list with other [old] browsers that are accessible and determine features that are required.

The easy way would be to require everything and cancel all scripts if suport for any feature is missing. This will also rule out IE6 and IE7 and opera below 9.something and firefox below 2.0 or including - I'm not sure.

Use a server side language to detect if it's the old safari based on user-agent and load a different javascript file

本文标签: javascriptalternative when an older browser does not accept jqueryStack Overflow