[英]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.