Javascript & Links: prevent second page reload

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • umamy
    New Member
    • Jul 2006
    • 1

    #1

    Javascript & Links: prevent second page reload

    Hi, I am writing code to do the followin:

    - user clicks a link
    - the page reloads and sets a cookie with a js function in the header. The function reloads the same page with the cookie set and points it to an anchor

    My problem is that after the page loads with the anchor, it also reloads again without the anchor because of the href="some page" part. I've tried changing that to nothing, or to include the anchor, but it messes up the functionality of the page.

    Here is my code:

    The javascript:
    [CODE=javascript]function SetCookieContac ts(cookieName,c ookieValue,nDay s) {
    var today = new Date();
    var expire = new Date();
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime( today.getTime() + 3600000*24*nDay s);
    document.cookie = cookieName+"="+ escape(cookieVa lue)
    + ";expires="+exp ire.toGMTString ();
    window.location = "vendors.asp#pl aceholder";
    }
    [/CODE]
    The call:
    [HTML]<a href="vendors.a sp" onclick="SetCoo kieContacts('sh ow_contact',<%= objRS_vendors(" id")%>,365)">(S how)</a>
    [/HTML]
    If anyone can help, I would really appreciate it.

    Thanks!
    Amy
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Originally posted by umamy
    My problem is that after the page loads with the anchor, it also reloads again without the anchor because of the href="some page" part. I've tried changing that to nothing, or to include the anchor, but it messes up the functionality of the page.
    Adding a 'return false' should solve that problem:
    [HTML]<a href="vendors.a sp" onclick="SetCoo kieContacts('sh ow_contact',<%= objRS_vendors(" id")%>,365); return false;">(Show)</a>
    [/HTML]

    Comment

    Working...