简体   繁体   English

单击页面加载上的HTML按钮/链接

[英]Click an HTML button/link on page load

I have implemented various methods on this forum, and cannot figure out a solution. 我已经在该论坛上实现了各种方法,但找不到解决方案。

I simply need to open a popup button on pageload. 我只需要在页面加载上打开一个弹出按钮。

Here is the test page: test page 这是测试页: 测试页

Here is the code that I am currently working with: 这是我目前正在使用的代码:

<script>
window.setTimeout('clickit()',5000);
function clickit(){
   location.href = document.getElementById("fmp-button");
}
</script>

<a href="#" rel="487d7c3e5cb0edeabb176a823a117ad8" id="fmp-button"><img src="https://assets.fortumo.com/fmp/fortumopay_150x50_red.png" width="150" height="50" alt="Mobile Payments by Fortumo" border="0" id="popup"/></a>

<script src='https://assets.fortumo.com/fmp/fortumopay.js' type='text/javascript'></script>

Any help would be greatly appreciated! 任何帮助将不胜感激!

Update: Solved. 更新:已解决。 (see correct answer below) (请参见下面的正确答案)

FYI, you may need to increase the number to 1000 or 2000. 500 does not work. 仅供参考,您可能需要将数字增加到1000或2000。500不起作用。

You can try this code: 您可以尝试以下代码:

window.setTimeout(clickit, 500);

function clickit(){
   document.getElementById("fmp-button").click();
}

Explaining what happens: 解释发生了什么:

1) .setTimeout receives a function. 1) .setTimeout接收一个函数。 You were passing a String ; 您正在传递String ; you can also pass an anonymous function and trigger your function from inside: 您还可以传递anonymous function并从内部触发函数:

window.setTimeout(function() { clickit(); }, 500);

2) document.getElementById("fmp-button") returns the element with the ID fmp-button . 2) document.getElementById("fmp-button")返回ID为fmp-button的元素。

3) .click method "simulates" user click (triggers the link, as if it was an user clicking) 3) .click方法“模拟”用户单击(触发链接,就好像是用户单击一样)

Fiddle: https://jsfiddle.net/mrlew/mvrvdL7c/ 小提琴: https : //jsfiddle.net/mrlew/mvrvdL7c/

Edited my answer 编辑我的答案

How about this? 这个怎么样?

<html>
<body onload="myFunction()">



<script>
function myFunction() {
    var myWindow = window.open("", "", "width=200,height=100");
}
</script>

</body>

 $(document).ready(function() { var id = '#dialog'; //Get the screen height and width var maskHeight = $(document).height(); var maskWidth = $(window).width(); //Set heigth and width to mask to fill up the whole screen $('#mask').css({'width':maskWidth,'height':maskHeight}); //transition effect $('#mask').fadeIn(500); $('#mask').fadeTo("slow",0.9); //Get the window height and width var winH = $(window).height(); var winW = $(window).width(); //Set the popup window to center $(id).css('top', winH/2-$(id).height()/2); $(id).css('left', winW/2-$(id).width()/2); //transition effect $(id).fadeIn(2000); //if close button is clicked $('.window .close').click(function (e) { //Cancel the link behavior e.preventDefault(); $('#mask').hide(); $('.window').hide(); }); //if mask is clicked $('#mask').click(function () { $(this).hide(); $('.window').hide(); }); }); 
 #mask { position: absolute; left: 0; top: 0; z-index: 9000; background-color: #000; display: none; } #boxes .window { position: absolute; left: 0; top: 0; width: 440px; height: 200px; display: none; z-index: 9999; padding: 20px; border-radius: 15px; text-align: center; } #boxes #dialog { width: 750px; height: 300px; padding: 10px; background-color: #ffffff; font-family: 'Segoe UI Light', sans-serif; font-size: 15pt; } #popupfoot { font-size: 16pt; position: absolute; bottom: 0px; width: 250px; left: 250px; } 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script> <div id="boxes"> <div id="dialog" class="window"> Your Content Goes Here <div id="popupfoot"> <a href="#" class="close agree">I agree</a> | <a class="agree"style="color:red;" href="#">I do not agree</a> </div> </div> <div id="mask"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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