admin管理员组文章数量:1356751
I'm working on a JavaScript function that opens an external link in a new tab. However, I need the tab to automatically close if the user is redirected to a native mobile app via deep linking. Here's my current code:
export const openExternalLink = (url: string) => {
const newTab = window.open(url, '_blank');
// Check if the new tab was opened but it is empty due to deep linking redirecting to native related app then close it
if (newTab) {
// Close the tab if it is empty (native app redirect)
}
};
I'm working on a JavaScript function that opens an external link in a new tab. However, I need the tab to automatically close if the user is redirected to a native mobile app via deep linking. Here's my current code:
export const openExternalLink = (url: string) => {
const newTab = window.open(url, '_blank');
// Check if the new tab was opened but it is empty due to deep linking redirecting to native related app then close it
if (newTab) {
// Close the tab if it is empty (native app redirect)
}
};
The problem is that when a user clicks the link, it opens the link in a new tab. However, if the device supports deep linking and the user is redirected to a native app, the tab remains open, even though it's empty or doesn't load anything.
What I've tried:
Checking if the newTab is loaded by inspecting its content, but I can't determine if the tab is redirected.
Trying to use newTab.close() but I’m not sure if there’s a reliable way to detect when the tab redirects to the app.
What I need: A reliable solution that detects when the external link redirects to a native app, and then closes the tab automatically.
Any ideas on how to solve this?
Share Improve this question asked Mar 30 at 20:00 RichardsonRichardson 2,3141 gold badge24 silver badges62 bronze badges1 Answer
Reset to default 0Solution turned out to be to open an external link in a new tab and monitor its content to close the tab if it redirects to an empty page or remains empty. This can be done using JavaScript and setting an interval to check the content of the new tab. Below is a working solution that opens a URL in a new tab and closes the tab if it remains empty (or redirects to an empty page like about:blank).
export const openExternalLink = (url: string) => {
// Open the link in a new tab
const newTab = window.open(url, '_blank');
if (newTab) {
// Set a timer to check if the tab's content is empty
const checkEmptyContent = setInterval(() => {
try {
// If the content is still 'about:blank' or the body is empty, close the tab
if (newTab.document.body.innerHTML.trim() === '' || newTab.location.href === 'about:blank') {
console.log('Tab content is empty or redirected to about:blank. Closing the tab.');
newTab.close(); // Close the tab
clearInterval(checkEmptyContent); // Stop checking after closing
}
} catch (e) {
// If the new tab is from a different domain, a cross-origin error might occur
// We handle the error to avoid breaking the script
console.error('Cross-origin error:', e);
}
}, 1000); // Check every second (adjust as needed)
}
};
本文标签:
版权声明:本文标题:reactjs - How to open an external link in a new tab and close it when it redirects to a native app? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743976393a2570896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论