javascript - 从缩短的链接中检索原始 URL
问题描述
我想在单击它并访问目的地之前从缩短的链接中检索原始 URL。
我有这个外部链接https://example.com/api.php?redirect=XXXXXXX重定向到https://new.com。如何使用 PHP 或 Javascript获取原始链接https://new.com 。
解决方案
const extractParams = (url) =>{
const search = url.split("?")[1];
return Object.fromEntries(
search.split("&").map(param => param.split("="))
)
}
window.addEventListener("load", ()=>{
// select all a tag containg word redirect in href attr
const links = document.querySelectorAll('a[href*="redirect"]')
links.forEach(el => {
el.addEventListener("click", (e) =>{
/// remove line below in your solution if you need to links works correctly
e.preventDefault();
const url = e.target.href;
const params = extractParams(url);
//belove you have access to redirect and any otter url params
console.log(params.redirect);
})
})
})
<a href="https://example.com/api.php?redirect=https://new.com">New</a>
<a href="https://example.com/api.php?redirect=https://ben.com">Ben</a>
<a href="https://example.com/api.php?redirect=https://den.com">Den</a>
推荐阅读
- typescript - 重构我的代码后的新问题:“无效的道具:道具的类型检查失败”
- javascript - 在Javascript中按html元素拆分
- svelte - ibm svelte组件正确使用
- python-3.x - warnings.filterwarnings("ignore") 不会忽略 logging.warning
- r - 我需要有关如何分析多项式 logit 的帮助
- html - 获取 CSS 中自定义滑块的活动状态
- spring - 检测表中插入数据并调用方法
- html - 创建一个只有 html 的三列长的嵌套表
- powershell - 相同的脚本,相同的控制台,为什么颜色在 Powershell 中有效但在 cmd.exe 中无效?
- python - 我的 pygame 代码允许点击。如何将点击输入到我的板阵列中,以便我可以检查获胜并让计算机移动?