I have a .html file.
In the file, there is a link like this:
<a href="https://www.someOtherWebsite.com/" onclick="clicked('https://www.MyWebsite.com/send_emai.php?subject=hello&body=test')">Some text</a>
The clicked is supposedly calling a JavaScript function which for now is in the header of the same file and looks like this (I got pieces of it from different places online):
<script type="text/javascript">
function clicked(url) {
// your server call
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
// open the link in new tab
window.open(url);
}
</script>
I really just need to navigate to the URL in the href, but hit this URL which sends me an email about it:
https://www.MyWebsite.com/send_emai.php?subject=hello&body=test
And don't need this function to return. Do I still need Ajax? And is this code correct or am I missing some syntax? I last used JavaScript and Axax 10 years ago so this is a bit of a struggle :)
Thanks!
What if you hit the url that sends the email first, then navigate away?
I don't know exactly how fetch works, but you could try something like this:
<a onclick="clicked('https://www.someOtherWebsite.com/')">Some text</a>
<script>
function clicked(url) {
// your server call
fetch('https://www.MyWebsite.com/send_emai.php?subject=hello&body=test')
.then(() => {window.location.href = url});
}
</script>
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.