将HTML中的``标签的`href`属性等待`onclick`中的异步函数返回?

huangapple go评论79阅读模式
英文:

Make html a tag href wait for async function in onclick to return?

问题

我有一个链接,其中有一个onclick,应该在href之前执行

<a class="btn btn-primary" href="?mode=full" onclick="req('name=vmode&amp;value=full','POST','/cookie')">Promeni rezim prikaza</a>

被调用的JavaScript函数的相关部分如下:

function req(data='', method='GET', url='', lang='', el='null') {
    const Http = new XMLHttpRequest();
    Http.open(method, url);
    // ...

    const e = document.getElementById(el);
    if (e && e.value != '' || !e)
        Http.send(msg);
    // 或者返回false,无论如何
}

现在只有在我设置以下代码时才起作用:

Http.open(method, url, false);

但是我的浏览器控制台会警告我,说在主线程上这样做已经不推荐了,而且即使我没有调用该部分(奇怪的是),它根本不允许我的脚本的一半工作。

如何正确地做到这一点?即使使它等待异步函数。

英文:

I have this link with an onclick that's supposed to execute before the href

&lt;a class=&quot;btn btn-primary&quot; href=&quot;?mode=full&quot; onclick=&quot;req(&#39;name=vmode&amp;value=full&#39;,&#39;POST&#39;,&#39;/cookie&#39;)&quot;&gt;Promeni rezim prikaza&lt;/a&gt;

And the relevant parts of the called js function looks like this:

function req(data=&#39;&#39;,method=&#39;GET&#39;,url=&#39;&#39;,lang=&#39;&#39;,el=&#39;null&#39;){
	const Http = new XMLHttpRequest();
	Http.open(method,url);

...

const e = document.getElementById(el);
	if (e &amp;&amp; e.value != &#39;&#39; || !e)
		Http.send(msg);

or return false whatever

Now this only works when I set

Http.open(method,url,false);

But I get warned from my browser's console about how doing this on the main thread is deprecated and it straight up doesn't allow half of my script to work even if I'm not calling that part(weird)

How does one properly do this? i.e make it await the async function

答案1

得分: 5

只需翻译代码部分:

Pass then `event` of the click to your function: `onclick=&quot;req(event, &#39;name=vm....`

Then your function can [`event.preventDefault()`](https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault) it, so that the default action of the click is not executed.

Now wait for your network request to return (probably want to use [`fetch()`](https://developers.google.com/web/updates/2015/03/introduction-to-fetch), that&#39;s much simpler than the old `XMLHttpRequest`).

Once the promise resolved, trigger the redirect to the target URL:

&lt;body&gt;
    &lt;a href=&quot;?mode=full&quot; onclick=&quot;req(event, &#39;name=vmode&amp;value=full&#39;,&#39;POST&#39;,&#39;/cookie&#39;)&quot;&gt;Promeni rezim prikaza&lt;/a&gt;
&lt;/body&gt;
&lt;script&gt;
function req(event, data=&#39;&#39;,method=&#39;GET&#39;,url=&#39;/index.html?test&#39;) {
    console.debug(event);
    event.preventDefault();
    fetch(url, {method: &quot;POST&quot;}).then(() =&gt; {
        location.href = event.target.href;
    });
}
&lt;/script&gt;
英文:

Pass then event of the click to your function: onclick=&quot;req(event, &#39;name=vm....

Then your function can event.preventDefault() it, so that the default action of the click is not executed.

Now wait for your network request to return (probably want to use fetch(), that's much simpler than the old XMLHttpRequest).

Once the promise resolved, trigger the redirect to the target URL:

&lt;body&gt;
    &lt;a href=&quot;?mode=full&quot; onclick=&quot;req(event, &#39;name=vmode&amp;value=full&#39;,&#39;POST&#39;,&#39;/cookie&#39;)&quot;&gt;Promeni rezim prikaza&lt;/a&gt;
&lt;/body&gt;
&lt;script&gt;
function req(event, data=&#39;&#39;,method=&#39;GET&#39;,url=&#39;/index.html?test&#39;) {
    console.debug(event);
    event.preventDefault();
    fetch(url, {method: &quot;POST&quot;}).then(() =&gt; {
        location.href = event.target.href;
    });
}
&lt;/script&gt;

But consider that your users may have to wait for the request in the background and don't understand what is going on.

If you only want to send some statistics data, you may want to consider using the new sendBeacon() API. It sends data (POST only) async in the background, so that the link target can be loaded for the user, while the browser works on the sendBeacon() requests in the background.

huangapple
  • 本文由 发表于 2020年1月6日 02:52:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定