Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prevent href from interrupting code by opening new window

    I have a code that launches all href links in a new window, but that's interfering with the newest code. There's one script that won't work properly because window.open() is interrupting it. I need the rest of the numerous links to launch as a separate window.

    HTML calling the function being interrupted:
    Code:
    //Expand all lists
    <a href="javascript:ddtreemenu.flatten('treemenu1', 'expand')">Expand All</a>
    JS launching the new window:
    Code:
    	$('a').each(function() {
    	   var a = new RegExp('/' + window.location.host + '/');
    	   if(!a.test(this.href) {
    		   $(this).click(function(event) {
    			   event.preventDefault();
    			   event.stopPropagation();
    			   window.open(this.href, '_blank');
    		   });
    	   }
    	});
    I tried changing the above if(!a.test(this.href) to if(!a.test(this.href) && a.test(this.href)!=="javascript:ddtreemenu.flatten('treemenu1', 'expand')") but it was unsuccessful.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,302
    Thanks
    13
    Thanked 345 Times in 341 Posts
    the simple fix is to not use a link for that particular case. you could use a <span> and make it look like a link with CSS.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    replace $('a') with
    Code:
    $('a[href^=http],a[href^=/]')
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Dormilich View Post
    the simple fix is to not use a link for that particular case. you could use a <span> and make it look like a link with CSS.
    span can't get the focus from the keyboard.

    The non href equivalent of the <a> tag is <button> (which can be styled to look like a link using CSS and can get the focus in all the same ways an <a> tag can)

    Better still is to actually use the href to perform an action for people who have JavaScript turned off. The click processing should be attached using an event listener with a preventDefault() call to stop the href from running when the javaScript does.

    href="javascript:... is intended for right clicking and adding the script to the bookmarks menu to run against other web sites. It should never be used for links that are intended to be left clicked.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,350
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by felgall View Post
    span can't get the focus from the keyboard.
    (almost) any tag can get focus if you give it a tabindex. tabindex=0 preserves the natural rotation.

    i tend to prefer buttons to do stuff and links to go places, but that was mostly from my experience over the years.

    now that we can use pushState or at least onhashchange(), it can sometimes make sense and provides great accessibility to use fragments/spoof REST urls to invoke actions.
    i think this is cleaner than off-sides click binding and all the event malarkey like preventDefault. Plus it lets the back button undo your "button click" and allows any code to invoke the click's action without reaching the element or handler function.
    something to think about...
    Last edited by rnd me; 02-14-2014 at 08:48 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Tags for this Thread

    Posting Permissions

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