3

So, I would like to be able to have people click on a link, and the an input field with a file will open. But I only want this to happen if the browser has support for it. As pointed out in this answer, chrome supports this. Firefox 3.6 does not, but Firefox 4 should.

I know you can frequently test for support of features in javascript, but I'm unsure how to test for this feature.

If you'd like to see what I mean, the below code shows the feature when clicking on the link. You can also play with this on my page.

<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Upload Field Click Test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script type="text/javascript">
        $(function() {
             var clicker = document.getElementById('clicker');
             var uploader = document.getElementById('uploader');
             clicker.addEventListener("click", function(e) {
                 uploader.click();
                 e.preventDefault();
             }, false);
        });
        </script>
    </head>
    <body>
        <form>
            <input type="file" id="uploader">
        </form>
        <a href="#" id="clicker">Should click the uploader</a>
    </body>
</html>

Things that do not work:

  1. testing !uploader.click
  2. seeing if uploader.click() throws an exception

1 Answer 1

0

You could use JQuery to dynamically write the HTML into the document at the appropriate place

$("#mylinkID").after('<a href=" ">Whatever</a>');`

and the link would be added after the element that contained the ID "mylinkID". If no support for JS, the link doesn't get displayed.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't want to detect whether the browser has javascript. I want to detect "Does the browser open the file upload dialog when I call the click function on a input[type="file"]?" and do it without sniffing the User-Agent
I'm guessing that the primary problem is the "addeventlistener" method which is not JQuery. I'm guessing that you would need a JQuery equivalent. See this thread at [link]stackoverflow.com/questions/2398099/… for details about this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.