javascript - document.getElementById().click(); dont works on mobile

I'm trying to run this, on desktop it works fine but it doesn't work on mobile devices
<div style="margin-top: 30px">
<p style="text-align: center; color: white; margin-bottom: 10px">Experimente o Wi-Fi gratuitamente! Curta e compartilhe nossa pГЎgina!</p>
<a id="freebot" class="button" onclick="sharePost();" href="#">Entre com o Facebook</a>
<a id="loginfree" href="$(link-login-only)?dst=$(link-orig-esc)&username=T-$(mac-esc)"></a>
</div>
This is the function I want to perform
function authHotspotMikrotik() {
document.getElementById("loginfree").click();
}
Can someone help me?
Answer
Solution:
If you're trying to simulate a mouse click, you need to create a mouse event and dispatch it.
let simulateClick = function(elem) {
// Create our event (with options)
let evt = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
// If cancelled, don't dispatch our event
let canceled = !elem.dispatchEvent(evt);
};
simulateClick(document.getElementById("loginfree"))
<div style="margin-top: 30px">
<p style="text-align: center; color: white; margin-bottom: 10px">Experimente o Wi-Fi gratuitamente! Curta e compartilhe nossa pГЎgina!</p>
<a id="freebot" class="button" onclick="sharePost();" href="#">Entre com o Facebook</a>
<a id="loginfree" href='https://google.com'>click element</a>
</div>
Answer
Solution:
I these code modifications:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Amazon Inter.Net Wi-Fi - Login</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!-- two other colors
<body class="lite">
<body class="dark">
-->
$(if chap-id)
<form name="sendin" action="$(link-login-only)" method="post" style="display:none">
<input type="hidden" name="username" />
<input type="hidden" name="password" />
<input type="hidden" name="dst" value="$(link-orig)" />
<input type="hidden" name="popup" value="true" />
</form>
<script src="/md5.js"></script>
<script>
function doLogin() {
document.sendin.username.value = document.login.username.value;
document.sendin.password.value = hexMD5('$(chap-id)' + document.login.password.value + '$(chap-challenge)');
document.sendin.submit();
return false;
}
</script>
$(endif)
<div class="ie-fixMinHeight">
<div class="main">
<div class="wrap animated fadeIn">
<form name="login" action="$(link-login-only)" method="post" $(if chap-id) onSubmit="return doLogin()" $(endif)>
<input type="hidden" name="dst" value="$(link-orig)" />
<input type="hidden" name="popup" value="true" />
<img class="logo" src="img/logo.svg" alt="#">
<p class="info $(if error)alert$(endif)">
$(if error == "")Digite seu e-mail e senha para utilizar o Wi-Fi da Amazon Inter.net$(endif)
$(if error)$(error)$(endif)
</p>
<label>
<img class="ico" src="img/user.svg" alt="#" />
<input name="username" type="text" value="$(username)" placeholder="E-mail" />
</label>
<label>
<img class="ico" src="img/password.svg" alt="#" />
<input name="password" type="password" placeholder="Senha" />
</label>
<p style="margin-bottom: 10px">
<a style="color: white" href="#">Esqueci minha senha?</a>
</p>
<p>
<input type="submit" value="Conectar" />
<a id="cadbot" class="button" href="#">Cadastre-se</a>
</p>
$(if trial == 'yes')
<script async defer src="js/sdk.js"></script>
<script>
var app_id = 'XXXXXXXXXXXXXXXX' // id do app criado no facebook
window.fbAsyncInit = function() {
FB.init({
appId : app_id,
autoLogAppEvents : true,
xfbml : true,
version : 'v3.3'
});
};
function sharePost(){
FB.ui(
{
method: 'share',
href: 'https://www.facebook.com/amazoninter.net.br',
},
// callback
function(response) {
if (response && !response.error_message) {
window.open('https://example.com/login?username=T-$(mac-esc)', '_self');
} else {
alert('Erro durante a postagem.');
}
}
);
}
</script>
<div style="margin-top: 30px">
<p style="text-align: center; color: white; margin-bottom: 10px">Experimente o Wi-Fi gratuitamente! Curta e compartilhe nossa pГЎgina!</p>
<a id="freebot" class="button" onclick="sharePost();" href="#">Entre com o Facebook</a>
</div>
$(endif)
</form>
<p class="info bt">Desenvolvido por Departamento de TI - Amazon Inter.Net</p>
</div>
</div>
</div>
</body>
</html>
If you look at the sharePost(); function, I open the link in the facebook callback, on the desktop it opens the link normally and replaces the php variable "$(mac-esc) with the MAC address of the device that shared the post and login is done, but on mobile devices after the post is done it apparently does not open the link and does not replace the variable "$(mac-esc)" with the MAC of the smartphone, it simply closes the TAB opened by the smartphone and goes back to the login screen. Then my question comes in, is the fact that the desktop opens a POPUP and the mobile device opens a NEW TAB could be the cause of the problem?
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreach() argument must be of type array|object, null given
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
CSS
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.