1

I have a Javascript function

function changeDocument(idxpv, open){
    if(open)
        $("#Upload"+idxpv + " input[type=file]").click();
}

and a link

<a href="javascript:changeDocument(1,true);" class="close fileupload-exists" 
   data-dismiss="fileupload">change document</a>

Now on clicking the link IE9/8 shows the prompt box asking for LEAVE THE PAGE and STAY ON PAGE

EDIT

The file upload button is generated from Valum's qqFileuploader library.

Could someone guide me whats wrong with it?

Padyster
  • 993
  • 3
  • 11
  • 21
  • @Padyster if `open === false` what should happen? IE must be trying to follow link and the still something running in the background... try adding `return false` to prevent the browser to change page – guiligan Aug 05 '13 at 16:26
  • 2
    Please provide where exactly said function is defined, if you click stay on this page look for any errors in the console, and provide any debugging that you have done on your own before coming here. – Kevin B Aug 05 '13 at 16:27
  • If You dont see any good reason to DOWNVOTE, then why did you downvoted without any comments? – Padyster Aug 05 '13 at 16:28
  • 1
    You know that there are multiple people that have a look at the question and that destroy (who made the comment) probably didn't downvote you? Calm down! – Felix Kling Aug 05 '13 at 16:29
  • @KevinB The Error console shows the error in the file which has nothing to do with this functionality. It shows error for some line in qqfileuploader.js file.. – Padyster Aug 05 '13 at 16:43
  • 2
    Don't use `javascript:` in the `href`. Attach the event "correctly", you *are* using jQuery, use `.click()`. Since you are following a link, the browser may think you are leaving the page and my trigger `onbeforeunload`. – gen_Eric Aug 05 '13 at 16:46
  • It could possible also be that you can't trigger your `file input` in IE8 / IE9 and create an error or something? – putvande Aug 05 '13 at 16:48
  • @RocketHazmat .click() is already used in function with input[type=file].. is there any other way to envoke file upload button? – Padyster Aug 05 '13 at 16:57
  • @Padyster: I meant to *assign* the event using `.click(function(){})`. – gen_Eric Aug 05 '13 at 17:24
  • You might run into some security issues later on, by the way, for triggering the `click` through javascript, rather than having the users actually clicking the input. If you do, look into using a label and moving the input off screen. That way clicking the label (which you can style) will click the input, as well. – Colin DeClue Aug 05 '13 at 17:51
  • 1
    Do you meant this [plugin valums/file-uploader](https://github.com/valums/file-uploader)? Which version are you using? And it [seems the main branch has a different name now](https://github.com/Widen/fine-uploader). – surfmuggle Aug 05 '13 at 18:10

1 Answers1

1

The call to $("#Upload1 input[type=file]").click(); is not executed because open is undefined.

I copied your code in this document:

  • clicking the link calls the function changeDocument()
  • but since open is undefined the click event is not fired

If you change the calling to this javascript:changeDocument(1, true); the click is fired and the file open dialog is called.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>html 5</title>   
    <script src="../jQuery/jquery_1.9.1.js"></script>
    <script>
        function changeDocument(idxpv, open){
        console.log("idxpv", idxpv);
        console.log("open", open);
        if(open)
            $("#Upload"+idxpv + " input[type=file]").click();
    }

    </script>
  </head>
  <body>
    <p>
    <a href="javascript:changeDocument(1, true);" class="close fileupload-exists" 
                data-dismiss="fileupload">change document</a>
    </p>                
    <p id="Upload1">    
    input type=file: <input type="file">                
   </p>
  </body>
</html>

If you have more questions feel free to ask.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77