Results 1 to 2 of 2

Thread: trigger click

  1. #1
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation trigger click

    Code:
    <head>
    <script type="text/javascript" src="jquery.js"></script>
    <style type="text/css">
    .test
    {
    position:absolute;
    left:300px;
    top:150px;
    }
    </style>
    <script type="text/javascript">
    
    var event = $.Event('event');
            event.clientX = 300;
            event.clientY = 150;
           
    $(document).ready(function(click){
      $("a").trigger('event');
    });
    
    </script>
    </head>
    <body>
    <a class="test" href="http://www.yahoo.com/" target="_blank">test</a>
    </body>
    i am trying to make it work, but its not working...
    what i want??
    i want that when my browser open the webpage, it should automatically click on "test" and open the hyper link
    please help...

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You might be able to fool some browsers with this sort of approach if the syntax is correct. But as a general rule the default setting for all browser's popup blockers is such that if the user doesn't initiate a new window or tab via a direct action on their part, it will be blocked.

    Like this works in IE, but only locally:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	$('#test').click(function(){
    		open(this.href, this.target);
    	}).trigger('click');
    });
    </script>
    </head>
    <body>
    <a id="test" href="http://www.yahoo.com" target="_blank">Yahoo</a>
    </body>
    </html>
    But it doesn't work in Firefox (blocks it) or Opera (does nothing), probably won't work in others either. And no matter how complex you make it, if the browser can tell that the user didn't click on the link, it will be blocked.

    Your best bet if you must do something like this is to flat out ask the user to click on the link or to trick the user into clicking on something that will give the desired result.

    In the first case it's obvious, something like:

    HTML Code:
    For illustrative purposes, please open this new window or tab:
    
    <a id="test" href="http://www.yahoo.com" target="_blank">Yahoo</a>
    In the second case, you can get creative, but some users will be scared off. Here's one way, I'm sure there are a 1000 or more others:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	$('#test').click(function(){
    		open('http://www.yahoo.com', '_blank');
    		$('#content').show();
    	});
    });
    </script>
    <style type="text/css">
    #content {display: none;}
    </style>
    </head>
    <body>
    <a id="test" href="#">Click For Page Content</a>
    <div id="content">Real Stuff is hidden here</div>
    </body>
    </html>
    Obviously, if the use disables javascript and css, or runs a bookmarklet to show the content div, or uses a diagnostic tool to set it's display to block, they will see the content without clicking. But few will figure that out right away. So, if you don't mind alienating some people, perhaps a high percentage of the people visiting that page, you can try it or a variation of it.
    Last edited by jscheuer1; 04-01-2012 at 06:13 PM. Reason: IE, but only locally
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •