PDA

View Full Version : trigger click



asprilla
04-01-2012, 04:49 PM
<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...

jscheuer1
04-01-2012, 05:31 PM
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:


<!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:


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:


<!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.