1

I have a logout link

  <div id="titleInfo">
      <div id="signOut">
          <a href="" class="signOutLink"  >Log Out</a>
      </div>
  </div>

I need to add navigation destination dynamically.

$(document).ready(function() {
    //Navigation Handling
    $('.signOutLink').click(function() {
        alert('h');
        window.location.href("LogOut.aspx");
        //window.location.replace("LogOut.aspx");

    });
});

But it is not working. How can we correct it?

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    `return false;` after the redirect statement – Mridul Aggarwal Nov 08 '12 at 10:29
  • What is the aim of this? If you add `href` attribute on a `click` event, it will not go to the logout page. If you add a `return false` in the end, it will never go there. If you do not add `return false`, it will go there on clicking for the second time! – Abhilash Nov 08 '12 at 10:33
  • @Abhilash the question is using `window.location.href`, it doesn't change the href of the link – Mridul Aggarwal Nov 08 '12 at 10:39
  • Also i'm not sure but i always used `window.location.href = url;`. (url pointing the full url) – Mridul Aggarwal Nov 08 '12 at 10:42

6 Answers6

3
$(document).ready(function () {
    $('a.signOutLink').click(function () {
        window.location = "LogOut.aspx";
    });
});
luc
  • 41,928
  • 25
  • 127
  • 172
1

Try this...

$(document).ready(function () {

$('.signOutLink').click(function () {
    window.location = 'LogOut.aspx';
});

);
Alex Gill
  • 2,395
  • 1
  • 16
  • 18
1
var a = document.getElementById('yourlinkId'); //or grab it by tagname etc
a.href = "somelink url"

See "How can I add “href” attribute to a link dynamically using javascript?" or you can use element.setAttribute (see Mozilla Developer Network's documentation).

<div id="titleInfo">
    <div id="signOut">
        <a href="" id="signOutLink"  >Log Out</a>
    </div>
</div>

$(document).ready(function () {
    //Navigation Handling
    $('#signOutLink').click(function () {
        alert('h');
        $('#signOutLink').attr("href", "LogOut.aspx");
     });
});
Community
  • 1
  • 1
Gadde
  • 1,451
  • 15
  • 37
0

$(document).ready(function () {

$('.signOutLink').attr('href', 'LogOut.aspx') 

});

Roman
  • 504
  • 4
  • 10
0

The reason I was asking for the end goal of this code is this:

If all you want is to go to a location on clicking a link and you want to assign the url dynamically, you don't need to do it in the click() event. It can be done simply by setting the property in the .ready() event lik so,

$('a.signOutLink').prop('href','LogOut.aspx'); // or using the .attr() method

That way, once you click on the link, it will just follow the url as it is set in the href

Abhilash
  • 1,610
  • 9
  • 19
0

Thanks to all. I used following:

$('.signOutLink').click(function () {
    window.location.href("LogOut.aspx?a=b"); //Can use REPLACE also
    return false;  //FALSE

});
LCJ
  • 22,196
  • 67
  • 260
  • 418