1

I'm very fresh to php/js, and need some help.

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.

So if the URL is "https://example.com/embed.php?ch=channel" I want "channel" to be pasted in the function.

The javascript provided is (can be found here)

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.

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?

  • what is `Embed`? what is the argument `embed` for? which part are you having issue with? creating the URL in javascript or "reading" the URL in PHP? – Jaromanda X May 13 '18 at 02:04
  • 1
    Possible duplicate of [How to get the value from the GET parameters?](https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters) – Trevor Clarke May 13 '18 at 02:10

2 Answers2

0

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 !

0

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>