How do I remove part of a string from a specific character? - javascript

I have the following url:
http://intranet-something/IT/Pages/help.aspx?kb=1
I want to remove the ?kb=1 and assign http://intranet-something/IT/Pages/help.aspx to a new variable.
So far I've tried the following:
var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"
if(link.includes('?kb=')){
var splitLink = link.split('?');
}
However this just removes the question mark.
The 1 at the end of the url can change.
How do I remove everything from and including the question mark?

Use the URL interface to manipulate URLs:
const link = "http://intranet-something/IT/Pages/help.aspx?kb=1";
const url = new URL(link);
url.search = '';
console.log(url.toString());

var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"
if (link.includes('?kb=')) {
var splitLink = link.split('?');
}
var url = splitLink ? splitLink[0] : link;
console.log(url);

var link = "http://intranet-something/IT/Pages/help.aspx?kb=1"
if(link.includes('?kb=')){
var splitLink = link.split('?');
console.log(splitLink[0]);
}

You can also try like this
const link = "http://intranet-something/IT/Pages/help.aspx?kb=1";
const NewLink = link.split('?');
console.log(NewLink[0]);

Related

Grab query string value from URL using Javascript

I want to visit https://example.com/?GCLID=test123 and store whatever is in GCLID in a variable.
How do I do this? The following keeps returning null
var url = window.location.href;
// test
url = "https://example.com/?GCLID=test123";
const params = new URLSearchParams(url);
var gclid = params.get('GCLID');
alert(params);
alert(gclid);
You have to take the part after '?' in new URLSearchParams, see below
example for same, i.e you will pass window.location.search like this
const params = new URLSearchParams(window.location.search);
var url = window.location.href;
// test
url = "https://example.com/?GCLID=test123";
const params = new URLSearchParams(url.split('?')[1]);
var gclid = params.get('GCLID');
alert(params);
alert(gclid);

Trying to split following url

I'm trying to split following URL:
http://www.store.com/products.aspx/Books/The-happy-donkey
in order to get only store.
How can this be done?
To do something as generic as possible I'd do something like:
const url = new URL('http://www.store.com/products.aspx/Books/The-happy-donkey');
const { hostname } = url;
const domain = hostname.match(/^www\.(\w+)\.com/);
console.log(domain[1]);
Use below code
//window.location.href
let str = "http://www.store.com/products.aspx/Books/The-happy-donkey";
const words = str.split('.');
console.log(words[1]);

Jquery how to visit url + var?

The text in my input is: test
What I want to do is end up with the next result:
some url + var
http://google.com/test
function off(){
var visit = $('#visit').val();
window.location = (visit);
}
Something like that:
window.location = "http://google.com/(visit)";
But the var is not acceptable
function off(){
var visit = $('#visit').val();
var url = "https://google.com/"
window.location = url + visit;
}
Simple concatenation where url is what you want the URL to be and visit is whatever you're appending
Is that what you want?
window.location = "http://google.com/" + visit;

get file name of selected file

How can I get only the name of the selected Data. I do the following but get the whole path of the File. I would like to display the filename for the user
var dialog = require('electron').remote.dialog;
var url;
document.getElementById('openButton').onclick = () => {
dialog.showOpenDialog((fileName) => {
if(fileName === undefined) {
alert('No file selected');
} else {
console.log(fileName)
url = fileName[0];
console.log(url);
$('#dataFileName').html(url)
}
})
};
What i get is "/Users/void/Desktop/abc.xlsx" and I would like to have in addition to that only the file i opened.
You can also use path.basename()
const {basename} = require('path')
let filePath = "/Users/void/Desktop/abc.xlsx"
let fileName = basename(filePath)
Here is a simple way you can grab just the file name:
var filePath = "/Users/void/Desktop/abc.xlsx";
var fileName = filePath.replace(/^.*[\\\/]/, '');
console.log(fileName);
Here is a fiddle to demonstrate.
If I understood correctly, you can use this:
var mystring = "/Users/void/Desktop/abc.xlsx"; //replace your string
var temp = mystring.split("/"); // split url into array
var fileName = temp[temp.length-1]; // get the last element of the array
What you basically do is to split your url with "/" regex, so you get each bit, and filename is always the last bit so you can get it with array's length you just created.

Get pathname along with PHP vars using JavaScript?

I want to save an entire URL paths to a variable, including the php vars, eg:
mysite.com/pagename?id=2
I can use
var pathname = window.location.pathname;
but this only retrieves the URL without the variables.
Is there a function to retrieve the URL as a literal string?
This should work
window.location.href
Have you tried see if it works:
document.URL
Can you try this,
// Get current page url using JavaScript
var currentPageUrl = "";
if (typeof this.href === "undefined") {
currentPageUrl = document.location.toString().toLowerCase();
}
else {
currentPageUrl = this.href.toString().toLowerCase();
}
Ref: http://www.codeproject.com/Tips/498368/Get-current-page-URL-using-JavaScript
It's hard , this answer explains how to implement it from the top response:
function getQueryParams(qs) {
qs = qs.split("+").join(" ");
var params = {}, tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while (tokens = re.exec(qs)) {
params[decodeURIComponent(tokens[1])]
= decodeURIComponent(tokens[2]);
}
return params;
}
//var query = getQueryParams(document.location.search);
//alert(query.foo);

Categories

Resources