SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Prevent Default

  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Prevent Default

    I am loading a link with ajax. When the link pops on the screen and I click it, I get redirected to my 404 page and my lightbox doesn't load.

    If the link pops in and I refresh my browser, then I click the link my lightbox will show up.

    How can I do a prevent default on the <a href> in pure JS? No frameworks please.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Code:
    stopDef = function(e) {
     if (e && e.preventDefault)
       e.preventDefault();
     else if (window.event && window.event.returnValue)
       window.eventReturnValue = false;
     };
    then just call stopDef() from within the JavaScript that is attached to the <a href>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Code:
    stopDef = function(e) {
     if (e && e.preventDefault)
       e.preventDefault();
     else if (window.event && window.event.returnValue)
       window.eventReturnValue = false;
     };
    then just call stopDef() from within the JavaScript that is attached to the <a href>
    That doesn't seem to be working for me. What am I doing wrong?

    Code JavaScript:
    if (info[x].reminder == 'assoc_requests')
    {
    	var a = document.createElement('a');
    	var a2 = document.createElement('a');
     
    	a.href = '/accountSettings.php?tab=6';
    	a.title = 'Invite friends!';
    	a.id = 'associateRequests';
    	a.href = 'void(0)';
     
    	a2.href = '/accountSettings.php?tab=6';
    	a2.title = 'Invite friends!';
    	a2.className = 'f_right';
    	a2.id = 'associateRequests2';
    	a2.href = 'void(0)';
     
    	appendText(row, 'You have ');
    	appendText(a, info[x].details);
    	row.appendChild(a);
     
    	if (info[x].details == 1)
    	{
    		appendText(row, ' Associate Request');
    	}
    	else
    	{
    		appendText(row, ' Associate Requests');
    	}
     
    	appendText(a2, 'View Associate Requests');
    	row.appendChild(a2);
     
    	list.appendChild(row);
     
    	stopDef();
     
    	stopDef = function(e) {
    	 if (e && e.preventDefault)
    	   e.preventDefault();
    	 else if (window.event && window.event.returnValue)
    	   window.eventReturnValue = false;
    	 };
    }

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    You need to define the stopDef function before you call it when you define it that way.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    You need to define the stopDef function before you call it when you define it that way.
    Even after I move the function call I still get redirected. Happen to have any other ideas?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •