[英]Paste part of URL into php/javascript function
I'm very fresh to php/js, and need some help. 我对php / js非常了解,需要一些帮助。
My URL goes like https://example.com/embed.php?ch=GOESHERE , and I am wanting to make the site figure out what the URL says after "?ch=" part and use it in a function. 我的URL像https://example.com/embed.php?ch=GOESHERE一样,我想让网站弄清楚URL在“?ch =“部分之后说什么,并在函数中使用它。
So if the URL is " https://example.com/embed.php?ch=channel " I want "channel" to be pasted in the function. 因此,如果URL是“ https://example.com/embed.php?ch=channel ”,我希望将“ channel”粘贴到函数中。
The javascript provided is ( can be found here ) 提供的javascript是( 可以在此处找到 )
new Twitch.Embed("twitch-embed", {
width: 1255,
height: 500,
channel: "channel"
});
I'd like whatever the ?ch=
url is to be where the channel: "channel"
currently is. 我想将
?ch=
url设置为channel: "channel"
当前所在的位置。
Sorry if I'm not making much sense, but I'm trying to explain what I can. 抱歉,如果我没有太多道理,但我想解释我能做些什么。
Appreciate all help. 感谢所有帮助。
EDIT I am using 编辑我正在使用
new URL(location.href).searchParams.get('ch')
const params = new URL(location.href).searchParams;
const ch = params.get('ch');
console.log(ch);
to find the param I need. 找到我需要的参数。 Now how can I paste this param into the
channel: "channel"
section where I need it? 现在如何将该参数粘贴到需要的
channel: "channel"
部分中?
you need to get value first with this 您需要首先获得价值
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, " "));
}
Usage 用法
// query string: ?ch=value
var ch = getParameterByName('foo'); // "value"
after that add your value to your array. 之后,将您的值添加到数组中。 tell me if its work !
告诉我它的工作!
Solved using 解决使用
<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.