SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prevent page link from working for POPUPS

    Hi can anyone help I want to open 'popups' using this unobtrusive JS. However the link also goes to the popup and doesn't cancel.

    I thought that by adding return false after the win.open command that the link wouldn't be followed but it is in Firefox. Any suggestions (without adding JS into my page structure)?

    Thanks
    Steve




    Code:
    function handleLink(e) {
      var el;
      if (window.event && window.event.srcElement)
        el = window.event.srcElement;
      if (e && e.target)
        el = e.target;
      if (!el)
        return;
    
      while (el.nodeName.toLowerCase() != 'a' &&
          el.nodeName.toLowerCase() != 'body')
        el = el.parentNode;
      if (el.nodeName.toLowerCase() == 'body')
        return;
    
      if (el.className && (" " + el.className + " ").indexOf(' popup ') !=-1) {
        
    	window.open(el.href, "popup", "top=50,left=50,width=400,height=600,toolbar=0,scrollbars=0,location=0,status=0,menubar=0");
    	return false;	
    	
    	
        if (window.event) {
          window.event.cancelBubble = true;
          window.event.returnValue = false;
    	  return false;
    	  
        }
        if (e && e.stopPropagation && e.preventDefault) {
          e.stopPropagation();
          e.preventDefault();
    	  return false;
        }
      }
    }
    
    function cancelClick() {  
        return false;
    
    }
    
    // here's the bit that installs the listeners
    
    function addListeners() {
      if (!document.getElementById)
        return;
    
      var all_links = document.getElementsByTagName('a');
      for (var i = 0; i < all_links.length; i++) {
        addEvent(all_links[i], 'click', handleLink, false);
        //all_links[i].onclick = cancelClick;
      }
    }
    
    addEvent(window, 'load', addListeners, false);

  2. #2
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone got any suggestions? can anyone help - I can't find a solution.

    Thanks
    Steven

  3. #3
    d^_^b Iain G's Avatar
    Join Date
    Nov 2005
    Location
    Gloucester, UK
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you may be over-egging the pudding slightly with all the careful event sniffing and the added complications are causing a problem somewhere. While commendable, it's much more efficient to use traditional event registration:
    Code:
    function handleLink(url)
    {
    	window.open(url, "popup", "top=50,left=50,width=400,height=600,toolbar=0,scrollbars=0,location=0,status=0  ,menubar=0");
    }
    
    window.onload = function()
    {
    	var all_links = document.getElementsByTagName('a');
    
    	for (var i = 0; i < all_links.length; i++)
    		all_links[i].onclick = function() { handleLink(this.href); return false; }
    }
    Coding and design philosophy:
    Keep It Simple, Stoopid

  4. #4
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but I need a way to target only those links with a class of 'popup' do you know how to do this?

    Steven

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function handleLink(url)
    {
     window.open(url, "popup", "top=50,left=50,width=400,height=600,toolbar=0,scrollbars=0,location=0,status=0	,menubar=0");
    }
    
    window.onload = function()
    {
     var all_links = document.getElementsByTagName('a');
    
     for (var i = 0; i < all_links.length; i++){
      if(all_links[i].className == "popup"){
       all_links[i].onclick = function() { handleLink(this.href); return false; }
      }
     }
    }
    I'm not very good with js but i think this will work, not tested it though.


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
  •