window.open() 阻止函数的其余部分运行

问题描述 投票:0回答:1

问题 我有一个功能,它首先将用户发送到一个新页面,然后在其上呈现信息。问题是使用“window.Open()”会重新加载页面并阻止其余功能运行

预期行为 “window.open()”将打开页面,但一旦打开就不会刷新它。

代码:

const ControlNavigationLinks = async function () {
  try {
    //  1) Redirect users to the pain page
    window.open("main.html", "_self"); //RELOADS THE PAGE, prevents function from continuing

    // 2)  Prevent The Search bar from reloading the page
    event.preventDefault();

    // 3) If Clicked on Logo, do nothing
    const element = event.target;
    if (element.classList.contains("logo")) return;

    // 4) Collect information about what country has been searched

    const form = document.querySelector(".search-bar-section");

    const search = form.elements["search"].value;
    if (search === null || "") return;

    model.state.search = await search;

    // 5) Clear DOM
    document.querySelector(".container-search").innerHTML = "";

    // 6) Get Information from External API about Countries
    const _ = await model.setSearchResults();

    // 7) Trigger Loading the page by calling seperate function
    ControlRenderSearchResults();
  } catch (e) {
    console.error(e.message);
  }
};

如果我删除“window.open()”,该函数将按预期执行(呈现信息),但我需要它切换到另一个页面并呈现信息。

我试过的

    //  1) Redirect users to the pain page
    window.open("main.html", "_self"); //RELOADS THE PAGE, prevents function from continuing

    window.preventDefault(); //Prevents window.open() from working
javascript window.open reloading
1个回答
0
投票

尝试从 window.open 方法中删除“_self”。

© www.soinside.com 2019 - 2024. All rights reserved.