简体   繁体   中英

How to automatically click on a HTML link when opening a new window?

I have a small program in JSP which basically when I click on a document, it opens a new window that allows me to view it. The problem here is it would not show the viewer unless I update or install adobe flash player.

I added a hyperlink link where I can easily click on it and it prompts me to "Allow" to view the document which is fine. The hyperlink looks like below:

<a href="https://get.adobe.com/flashplayer" >Enable Flash</a>

<a href="https://get.adobe.com/flashplayer"><img border="0" alt="Enable Flash" src="enable_flash.gif"/></a>

Now, I have to manually click on it, is there a way I can have the hyperlink auto clicked when the pop up windows shows?

I am new to JavaScript and HTML so I figured there is something that I could use like <body onload > .

Edit

This is how my code looks like now:

<body onload="Auto()" > <!--oncontextmenu="return false;"-->

<script>
    function Auto(){
    <a href="https://get.adobe.com/flashplayer" >Enable Flash</a>

        <a href="https://get.adobe.com/flashplayer"><img border="0" alt="Enable Flash" src="enable_flash.gif"/></a>

    }


</script>

Am I doing anything wrong?

You can simply use click() method

Add id to anchor element

<a href="https://get.adobe.com/flashplayer" id="myLink" >Enable Flash</a>

Add to your script

 function automateClick() {
    document.getElementById('myLink').click()
 }
 window.addEventListener("load", automateClick);

You can do this with as little as one line, if you want to learn more about the approach I've used, then it may be worth your time looking into functions such as querySelector . Once you have the desired element, you can then simply fire the click method like so.

 <a href="https://get.adobe.com/flashplayer">Demo</a> <script type="text/javascript"> document.querySelector('a[href="https://get.adobe.com/flashplayer"]').click(); </script> 

Yes..you can use below code and load your link there

$(document).ready(function() { 
 document.querySelector('a[href="https://get.adobe.com/flashplayer"]').click(); 
});

OR

$(function() { 
 document.querySelector('a[href="https://get.adobe.com/flashplayer"]').click(); 
});

OR

window.onload = function() {
   document.querySelector('a[href="https://get.adobe.com/flashplayer"]').click();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM