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

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.

Related

Adjusting video src to URL parameter

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');

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('?')));

How to obtain one part of the url using javascript only

This is about a password reset link sent to the email from firebase. I want to get the oobCode from it. But when I obtain the url of the page , it gives me the local address not the actual link I clicked in my email.Please help
https://example.com/usermgmt?mode=resetPassword&oobCode=ABC123&apiKey=AIzaSy...&lang=fr
This link actually loads the reset_pwd.html in my website. So when I try to get window.location.href , it doesnt give me the actual address displayed in the address bar.
This is what I have tried so far. But I realised I' m trying to split the local address of the web page http://127.0.0.1:80/reset_username
function func() {
var s = window.location.href;
ResponseURL = window.location.href;
var domain = ResponseURL.split('=');
alert(s);;
}
You can get it using URL.searchParams.get():
function getUrlParam(key, urlString = window.location.href) {
let url2 = new URL(urlString);
return url2.searchParams.get(key);
}
const url = `https://example.com/usermgmt?mode=resetPassword&oobCode=ABC123&apiKey=AIzaSy&lang=fr`;
console.log(getUrlParam('oobCode', url));
// Can be used without providing the URL, like:
// getUrlParam('oobCode');

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"]);
}

JS: get hostname from url , regex not working

I am trying to get hostname from set of urls that my webapp can encounter with.
The desired output should be something like http://localhost/Webapp/, ending at /Webapp/ and everything after that should be removed.
Kindly note that I dont want to use word Webapp in regex as this name is dynamic and used for demo/testcase only.this can be anything , not harcoded.
In real example I am using location.href.replace(/index.+/g, "").replace(/#.+/g, "")
and I want to keep only hostname ending atWebapp/.
Problem:
my solution seems to working fine except "http://localhost/Webapp/#" is not working correctly ? why is that ? see fiddle below
JSFIDDLE: http://jsfiddle.net/bababalcksheep/um0uqb8v/
JS:
var getHost = function (url) {
return url.replace(/index.+/g, "").replace(/#.+/g, "")
};
var urls = [
"http://localhost/Webapp/",
"http://localhost/Webapp/#",
"http://localhost:8080/Webapp/#sdf#dfgdf#fdg",
"12.168.1.1:8080/Webapp/index.html#",
"https://localhost/Webapp/index.html#ab#bg",
"https://localhost/Webapp/index.html"
];
//Print all urls
$.each(urls, function () {
$("<p/>").text(getHost(this)).appendTo($(".test"));
});
Use url.match(/https?:\/\/([^\/]+)/);
EDIT:
It returns an array where the 1st element is the host with protocol and the 2nd without.
You can try removing anything after the last slash (files and hash-es):
var getHost = function (url) {
return url.replace(/\/[^/]*?$/, '/');
};
And here's the updated fiddle.
There's a bit of a trick you can use to get the browser to extract the hostname for you.
var getHost = function (url) {
var a = document.createElement('a');
a.href = url;
return a.hostname;
};
It also appears you want the path as well. You can access it with the pathname property of the a element. If you're doing that, you ought to rename the function to something like getHostAndPath().

Categories

Resources