Andy Foster
Andy Foster

Reputation: 160

Modifying link behavior based on keypress

I have a link in each table row to view a record in more detail. I want to add ways to view the different pdf's we generate for each record, without growing the table too much. I've bound different events for different conditions of the click, but I'm having a few issues. First of all, it won't go to the pdf like it normally does from a hard link, it opens the save dialog. Secondly, once I tell it not to save, the page won't follow any of the pdf links at all. What am I doing wrong? I thought the prevent default might stop chrome from executing a special activity when it sees a keypress+click, which I'm not even sure was the problem to begin with. Strangely, after I, for example, ctrl-click, the page won't do anything, but mouseover shows the 'href' to be the ctrl-click modified location, and a regular click after that does what I wanted it to do in the first place.

looking at the code, I can see why it's doing what it's doing. The prevent default stops chrome from saving, like i wanted. The href is changed, like I told it. Now I have to follow the link. If I stumble across the answer before i get any replies, I'll answer.

$(".doThings").click(function(e){ 
    var ID = $(this).text();
    if(e.ctrlKey){
        event.preventDefault();
        $(this).attr("href", "http://foo.com/report.php?id="+ID);
    } 
    else if(e.altKey) {
        event.preventDefault();
        $(this).attr("href", "http://foo.com/r30.php?id="+ID); 
    } 
    else if(e.shiftKey) { 
        event.preventDefault();
        $(this).attr("href", "http://foo.com/r45.php?id="+ID); 
    } 
}); 

Upvotes: 1

Views: 77

Answers (2)

Andy Foster
Andy Foster

Reputation: 160

Changing the href was not the way to go. I just did a javascript redirect.

if(e.ctrlKey){
        e.preventDefault();
        window.location = ("http://foo.com/report.php?id="+ID);
    } 

Upvotes: 0

xdazz
xdazz

Reputation: 160923

Use window.location.href instead.

$(".doThings").click(function(e){ 
    var ID = $(this).text();
    if(e.ctrlKey){
        event.preventDefault();
        window.location.href = "http://foo.com/report.php?id="+ID;
    } 
    else if(e.altKey) {
        event.preventDefault();
        window.location.href = "http://foo.com/r30.php?id="+ID; 
    } 
    else if(e.shiftKey) { 
        event.preventDefault();
        window.location.href = "http://foo.com/r45.php?id="+ID; 
    } 
}); 

Upvotes: 1

Related Questions