Adjusting video src to URL parameter - javascript

I'd like to set the src to my video according to the URL parameter (after the ?)
Here's what I've got so far:
<video></video>
<script>
document.getElementsByTagName("video").src="###";
I need help with setting the ### src value to the correct value like so:
realistic example
https://website.com/player?##$%
src="https://areallylongaddress.com/videosourcefolder/##$%";
simpler example
https://website.com/player?intro_video
src="https://areallylongaddress.com/videosourcefolder/intro_video";
essentially, a strip of code that sets the video src to a fixed string value (areallylongaddress.com), then adds the url parameter to the end

If i understand you correctly u want to set the src of the video to the url parameter(query string).
Usually query strings are link this - "https://website.com/player?srcName=intro_video" So all u need to do is
const queryString = window.location.search;
// querySting = ?srcName=intro_video
const urlParams = new URLSearchParams(queryString);
const src_name = urlParams.get('src_name');
console.log(src_name);
//prints "intro_video"
document.getElementsByTagName("video").src=src_name;
Edit -
//main Html form where u go to https://website.com/player?folders/vkxq2f.mp4 page
let vid_URL = "https://files.catbox.moe/vkxq2f.mp4";
let vid_name = vid_URL.split('/').at(-1);
console.log(vid_name);
//here you do something like onclick(window.href=`https://website.com/player?video=${vid_name}`)
//Then in player.html
document.getElementsByTagName("video").src = new URLSearchParams(window.location.search).get('video');

Related

How to Get 10 from this link (https://google.com/?ib=10)

Check this image
I have this link in (note this link not from search URL)
var link = https://google.com/?ib=10 in main.js file.
Now how to get that 10 from this link in javaScript on Page load
I have tried this way
var link1 = link;
const url3 = new URLSearchParams(link1);
const ur = url3.get("ib");
var finaltgid = ur;
alert(ur);
But its not working may be this code only work when we use window.location.search
Instead of var or const
urlsearchparams does not parse full url, you need to pass only query part, like this:
var link = 'https://google.com/?ib=10';
const url = new URLSearchParams(new URL(link).search);
const ib = url.get("ib");
console.log(ib);
URLSearchParams only accepts the actual query part in the constructor. You are including the full link.
Edit: For a given link you can just supply the link in place of document.location (which directly fetches the pages current location)
Considering this as Vanilla JS. You can do the following.
let params = (new URL(document.location)).searchParams;
let ib = parseInt(params.get('ib')); // is the number 10
Note, using the parseInt to get the value as a number.

How to fetch appended part of url using javascript

How can we get part of string added to the url using java script.
example: my url link is in below format:
https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale
i tried below ways.
var v1=window.location.href
var v2=window.location.host
var v3=window.location.hostname
var v4=window.location.protocol
var v5=window.location.pathname
var v6=window.location.search
var v7=window.location.hash
but i am unable to get "testrecordname/idoftestrecord/selectedrecord" this portion of url.
can anyone suggest how to attain it
You need to manipulate the pathname
const url = new URL("https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale")
console.log(url.href);
console.log(url.host);
console.log(url.hostname);
console.log(url.protocol);
console.log(url.pathname); // this???
console.log(url.pathname.split("/s/")[1]); // for example
console.log(url.search);
console.log(url.hash);
One way could be extracting from URL string using string functions
let s = 'https://domain/imp/s/testrecordname/idoftestrecord/selectedrecord?languagelocale';
console.log(s.slice(s.indexOf('s/')+2,s.indexOf('?')));

Catching a param from URL by using Node.js

I have a constant link looking like this:
http://link.com/?val1=val1&val2=val2
And this link redirects me to a new link with a random value of a constant param such like;
http://link2.com/?constant=randomvalue/
Each time I use the first link, I get a random value from the following link.
By using Node.js, how can I catch the 'randomvalue' of 'constant' in the second link?
I have to use the first link to reach the second one.
Try reading the second link as a URL
let secondURL = new URL("http://link2.com/?constant=randomvalue/");
Then extract the value of the constant searchparam like so
let constantValue = secondURL.searchParams.get("constant"); //"randomvalue/"
#Misantorp's answer is probably best, but there is another way to do it. Check out the querystring module built into Node, it has a convenient parse method just for things like this: https://nodejs.org/api/querystring.html
This should work:
const querystring = require('querystring');
querystring.parse("http://link2.com/?constant=randomvalue/"); // { 'http://link2.com/?constant': 'randomvalue/' }
You may want to substring from the ? onwards to make it more clear:
const str = "http://link2.com/?constant=randomvalue/";
const paramIndex = str.indexOf("?");
if (paramIndex >= 0) {
const queryParamStr = str.substr(str.indexOf("?"));
const queryParams = querystring.parse(queryParamStr);
console.log(queryParams["constant"]);
}

Get last segment of url without parameters in javascript?

Ho do I get the last part of a URL segment excluding any URL parameters.
So if I'm on this page:
http://example.com/myfolder/mypage.aspx?x=1
I want to get:
mypage.aspx
You can do something this way:
// The first thing is to strip off the things after query string:
var url = "http://example.com/myfolder/mypage.aspx?x=1";
url = url.split("?")
url = url[0];
// Get the last path:
url = url.split("/");
page = url[url.length-1];
alert(page);
The above path will work even if there's no ? in it.

Jquery extract string from url and load remote element

I'm new in Jquery, I would like to have Jquery code to get the current page url and if the url contains certain string then load remote element.
example:
i have the page urls like this:
"http://......./Country/AU/result-search-to-buy"
"http://......./Country/CA/result-search-to-buy"
"http://......./Country/UK/result-search-to-buy"
the part "/Country/AU" is what I need to determine which page element I should load in, then if "AU" I load from "/state-loader.html .state-AU", if "CA" I load from "/state-loader.html .state-CA"
I have a builtin module "{module_pageaddress}" to get the value of the current page url, I just dont know the Jquery logic to let it work.
I expect something like this:
if {module_pageaddress} contains "/Country/AU/"
$('#MyDiv').load('state-loader.html .state-AU');
if {module_pageaddress} contains "/Country/CA/"
$('#MyDiv').load('state-loader.html .state-CA');
You can try the following if the country string is always last before the last slash in the URL
See a live fiddle
var url = "http://......./Country/AU/result-search-to-buy"
var loc = url.split( '/' );
//should be using the next line in live
//var loc = window.location.pathname.split( '/' );
var country = alert(loc [loc.length-2]);

Categories

Resources