[英]Confused on my JavaScript code when calling a URL and not sure if I need an Ajax call
I have a .html file.我有一个 .html 文件。
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): clicked 应该调用一个 JavaScript 函数,它现在位于同一个文件的标题中,看起来像这样(我从网上的不同地方得到了它的片段):
<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:我真的只需要导航到 href 中的 URL,但是点击这个 URL,它会向我发送一封关于它的电子邮件:
https://www.MyWebsite.com/send_emai.php?subject=hello&body=test
And don't need this function to return.并且不需要这个函数返回。 Do I still need Ajax?
我还需要 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 :)
我上次使用 JavaScript 和 Axax 是在 10 年前,所以这有点困难:)
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:我不知道 fetch 是如何工作的,但你可以尝试这样的事情:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.