[英]Paste part of URL into php/javascript function
我對php / js非常了解,需要一些幫助。
我的URL像https://example.com/embed.php?ch=GOESHERE一樣,我想讓網站弄清楚URL在“?ch =“部分之后說什么,並在函數中使用它。
因此,如果URL是“ https://example.com/embed.php?ch=channel ”,我希望將“ channel”粘貼到函數中。
提供的javascript是( 可以在此處找到 )
new Twitch.Embed("twitch-embed", {
width: 1255,
height: 500,
channel: "channel"
});
我想將?ch=
url設置為channel: "channel"
當前所在的位置。
抱歉,如果我沒有太多道理,但我想解釋我能做些什么。
感謝所有幫助。
編輯我正在使用
new URL(location.href).searchParams.get('ch')
const params = new URL(location.href).searchParams;
const ch = params.get('ch');
console.log(ch);
找到我需要的參數。 現在如何將該參數粘貼到需要的channel: "channel"
部分中?
您需要首先獲得價值
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
用法
// query string: ?ch=value
var ch = getParameterByName('foo'); // "value"
之后,將您的值添加到數組中。 告訴我它的工作!
解決使用
<script type="text/javascript">
new URL(location.href).searchParams.get('ch')
const params = new URL(location.href).searchParams;
const ch = params.get('ch');
new Twitch.Embed("twitch-embed", {
width: 1225,
height: 500,
channel: ch
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.