简体   繁体   中英

File download with javascript/jquery

I'm trying to download an apk file from my webapp using a tablet.

<a href="#" data-role="button" id="loginButton" class="pulsantino">Login</a>

$("#pulsanteLogin").live("click",function(){
     alert("1");
     window.location.href = 'http://xx.yy.zz.www:1234/staticResources/Myapp_version.apk';
     alert("2");
}

I know for sure that the url is correct (launching it from the browser will correctly start file download), but when i click on the button i get no error and no file download. I get both the alerts for '1' and '2'.

I've tried also with

location.href = 'url';

and

window.location = 'url';

Ideas?

Maybe this is too obvious, but why dont you just use a regular link like this directly:

<a href="http://xx.yy.zz.www:1234/staticResources/Myapp_version.apk" data-role="button" id="loginButton" class="pulsantino">Login</a>

Are you executing more code which requires the dynamic change of this link's href attribute?

Are you using a newer version of jQuery than 1.7? .live() got deprecated, and .on() is now the way to go. http://api.jquery.com/on/

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live(). Source: http://api.jquery.com/live/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM