JavaScript Automatic Links Clicking to Prevent Browser Bypass Access

  • 2021-07-12 05:18:48
  • OfStack

To do Alipay account login, you need to do 1 effect, that is, when you open the link page, you don't need to click on the link and jump directly to Alipay login page. That is to say, you need to do an automatic click link effect.

Basically, this is used:


<body onLoad="autoclick('auto')">

<a id='auto' href=".$url."><img border='0' src='images/alipaylog.gif' /></a>

</body>

<script type="text/javascript">


 function autoclick(){

 lnk = document.getElementById("auto");

 lnk.click();

  }

</script>

This can be used under IE, but other browsers can't. It also turns all over the place, wasting everyone's search time.

The following one is more reliable, let's take a look first:


<body onLoad="autoclick('auto')">

<a id='auto' href=".$url."><img border='0' src='images/alipaylog.gif' /></a>

</body>

<script type="text/javascript">  1: 

function autoclick(name)

{  

  if(document.all)  

  {  

    //alert(1);

     document.getElementById(name).click();  

  }  

   else  

  {  

    var evt = document.createEvent("MouseEvents");  

      evt.initEvent("click", true, true);  

    //alert(2);

    document.getElementById(name).dispatchEvent(evt);  

   }  

} 

</script>

This is normal under Chrome and IE, but not under Firefox. But it's better than the first one.

dispatchEvent is problematic under Firefox. Here is the solution:


document.getElementById("me").onclick = function() {

  var card = document.getElementById("card");

   if(document.createEvent){

    var ev = document.createEvent('HTMLEvents');

    ev.initEvent('click', false, true);

    card.dispatchEvent(ev);

   }

   else 

     card.click();
 }

Where the card element is an element with an event tied to it. The me element is the element that you want to call the click event of card by clicking on the me element. . . .

The point of the problem is that the js engine of firefox needs to create an event first: var ev = document. createEvent ('HTMLEvents');

Then specify the event as an click event: ev. initEvent ('click', false, true);

Finally, the event is paid to the card element: card. dispatchEvent (ev);

card = document.getElementById('id');

var ev = document.createEvent('HTMLEvents');

ev.initEvent('click', false, true);

card.dispatchEvent(ev);

Thus, dispatchEvent is the last step in the event delegate, linking the delegate event to the invoked element to achieve the effect of invoking the event of this element.

Finally, it is recommended to implement it with input. The following is the final solution:


<body onLoad="autoclick('auto2')">

 <input id="auto2" type="hidden" onClick="javascript:location.href = '&lt;?=$url?>' " />

</body>

<script type="text/javascript">  1: 

function autoclick(name)

 {  

  if(document.all)  

  {  

    //alert(1);

     document.getElementById(name).click();  

  }  

   else  

  {  

   var evt = document.createEvent("MouseEvents");  

     evt.initEvent("click", true, true);  

    //alert(2);

    document.getElementById(name).dispatchEvent(evt);  

   }  
} 
</script>


Related articles: