Change Link After Action via Jquery

Go To StackoverFlow.com

0

I have a download link, and after the user downloads it I want to remove the link to the download and change the text. I was able to get that going, but it does it so fast that the user would be unable to download the exe file. If I somehow delayed it would that be an solution? Is there a better way to do what I'm doing?

Here's my JS

function removeLink(){
    try{
        $('#downloadLink').text("Thank You For Downloading This Software!");
        $('#downloadLink').attr('href', 'javascript:void(0)');
    }
    catch(err){
        alert(err.message);
    }
}

link

<a href="download.exe" title="Download" id="download" onclick="removeLink()">Download</a>

Also I know it's not a very 'secure' way of doing it since I'm just using javascript for it, but this is mostly just for the average user.

2012-04-05 17:51
by Howdy_McGee
How is removeLink() called - amit_g 2012-04-05 17:55
Just make sure you have your bases covered - what happens if the user clicks download, cancels it, and then wants to try again - Alain 2012-04-05 18:22


1

Something like this?

$('#downloadLink').click(function() {
  $(this).replaceWith('<span>Thanks for downloading...</span>');
});

If you still have issues with it not working, put a window.location = 'download.exe'; at the top of the click function.

2012-04-05 17:57
by Brendan
That worked like a charm. I had to put window.location at the top of the function but the replaceWith() is a cool function I didn't know about! Thanks - Howdy_McGee 2012-04-05 18:23


0

So - knowing its not a secure method - what's wrong with:

$('a').on('click', function() {
    $(this).text('Thank You For Downloading This Software!').removeAttr('href');
});​

Change the selector to whatever is applicable instead of $('a')

2012-04-05 17:58
by mikevoermans
The problem is, it doesn't get a chance to download. It will change the text and href but the exe file won't download which I am assuming because the javascript is too fast(? - Howdy_McGee 2012-04-05 18:18


-2

You may not want function removeLink() becuase you are not calling it. Instead use

$(document).ready(){

});

And also for your time out issue...

use jQuery's delay()

Another thing is that you are calling #downloadLink... Don't forget to add the ID to the tag.

EDIT: As the others are saying you may just want to do the entire function in jQuery.

2012-04-05 17:58
by 4WebDev
Ads