【问题标题】:Paste part of URL into php/javascript function将部分 URL 粘贴到 php/javascript 函数中
【发布时间】:2018-05-13 02:02:42
【问题描述】:

我对 php/js 很陌生,需要一些帮助。

我的 URL 类似于 https://example.com/embed.php?ch=GOESHERE,我想让网站弄清楚 URL 在 "?ch=" 部分之后的内容并在函数中使用它。

因此,如果 URL 是“https://example.com/embed.php?ch=channel”,我希望将“频道”粘贴到函数中。

提供的javascript是(can be found here)

new Twitch.Embed("twitch-embed", {
  width: 1255,
  height: 500,
  channel: "channel"
});

我希望 ?ch= 网址位于 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" 部分?

【问题讨论】:

标签: javascript php html twitch


【解决方案1】:

你需要先通过这个获得价值

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"

然后将您的值添加到您的数组中。告诉我它是否有效!

【讨论】:

  • 请向可能还不知道的人解释一下这个函数的作用!
  • 每个人都是新手,我们需要帮助,我需要指出我可以讨论更多:(
  • @TrevorClarke 用另一个问题更新了我的 OP。
  • @JanHenning 假设您已经运行了您在编辑中放置的代码,您所要做的就是输入 channel: ch 而不是 channel: "channel" 希望它能正常工作
  • @TrevorClarke 啊,明白了!我忘了删除引号。非常感谢!像魅力一样工作。
【解决方案2】:

解决了

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-26
    • 2017-06-02
    • 1970-01-01
    • 2023-03-29
    • 2016-09-03
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多