我嘗試用“?”分割網址 并在 html 上使用第二個元素
例子:
https://url/page?google.com
我想收到的輸出是:google.com
并將頁面重定向到輸出,我使用的是 webflow,所以如果有人可以幫助撰寫完整的腳本,那就太棒了。
我試過了:
window.location.replace(id="new_url");
let url = window.location;
const array = url.split("?");
document.getElementById("new_url").innerHTML = array[1];
但它不起作用:(
uj5u.com熱心網友回復:
window.location.replace(id="new_url");不是有效的語法。
window.location.replace(new_url);其中 new_url 包含有效 URL 將立即更改頁面并忽略其后的所有其他腳本。
我假設您可以使用 URL api?
請注意,您的引數是非標準的,您需要添加協議 (https://) 才能轉到 URL
這是一個復雜的版本,但使用標準工具
const urlString = "https://url/page?google.com"
const url = new URL(urlString)
console.log(url.toString())
const firstSearchKey = [...url.searchParams.entries()][0][0]; // normally parameter=value
console.log(firstSearchKey)
location.replace(`https://${firstSearchKey}`)
這是一個更簡單的版本
const urlString = "https://url/page?google.com"
const [origin,passedUrl] = urlString.split("?");
location.replace(`https://${passedUrl}`)
uj5u.com熱心網友回復:
嘗試這個
const url = window.location.search.split("?")[1]
window.location.href = url
uj5u.com熱心網友回復:
let url = "https://url/page?google.com"
const regex = /\?(.*)/;
let res = regex.exec(url)
console.log(res[1])
uj5u.com熱心網友回復:
這是你想要的嗎?
const inputUrl = window.location.href // ex. https://url/page?google.com
const splitUrl = inputUrl.split("?") // = ["https://url/page", "google.com"]
const targetUrl = splitUrl[1] // = "google.com"
window.location.href = targetUrl // sets current window URL to google.com
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/522460.html
上一篇:奇怪的rb.velocity.y
