How to split domain with http or https in nodejs - javascript

Anyone can help to split the domain name with http or https from url string,
URL : https://www.test.com/abc/?a=1&b=1
Expected Output : https://www.test.com
Thanks in advance.

I strongly recommend you avoid using a home-grown regexp. Instead, use the node URL class:
https://nodejs.org/api/url.html
Not exactly sure which parts you want to keep or not (do you want to include the port? Do you want to decode IDNs?), but origin may be the way to go. Here’s the example straight out from the docs:
const { URL } = require('url');
const myURL = new URL('https://example.org/foo/bar?baz');
console.log(myURL.origin);
// Prints https://example.org
Otherwise, you could use the protocol and host or hostname components.

You can use the url-parse package also for get the origin from URL,
Refer : https://www.npmjs.com/package/url-parse
var URL = require('url-parse');
const url_obj = new URL('https://test.com/abc/?a=1');
console.log(url_obj.origin); // https://test.com

Related

Why can't update url protocol when old protocol include '+' in nodejs

Why can't update url protocol when old protocol include '+'
here is my demo test code
let u = new URL( 'git+https://url-fake-hostname/zh-TW/scripts')
console.log(u)
u.protocol = 'http:';
console.assert(u.protocol !== 'git+https:', u.protocol)
URL is a special object in Node.js, since Node.js want to make it browser-compatible.
There have two kind of method to build URL object
WHATWG URL API new URL(url) - used by web browsers
Legacy API require('url').parse(url) - Node.js specific
As document mentions:
The WHATWG URL Standard considers a handful of URL protocol schemes to be special in terms of how they are parsed and serialized. When a URL is parsed using one of these special protocols, the url.protocol property may be changed to another special protocol but cannot be changed to a non-special protocol, and vice versa.
Here is some example of same case that you had met:
const u = new URL('http://example.org');
u.protocol = 'https';
console.log(u.href);
// https://example.org
const u = new URL('http://example.org');
u.protocol = 'fish';
console.log(u.href);
// http://example.org
You can solve this problem by calling Legacy API:
const url = require('url');
let u = url.parse( 'git+https://url-fake-hostname/zh-TW/scripts')
u.protocol = 'http:';
console.log(u.protocol);// protocol: 'http:'

JS - baseURLString when sending pathnames only

I am trying to create a new URL in JS so it can be manipulated for an async request. As nothing is cross-origin (I think this is the correct usage of that term), the URLs I send for async request look like /MyLoginUrl or /MyUpdateDataUrl, etc. (i.e. I am only sending the pathname).
My attempt to create a new URL from an existing url looked basically like this:
// Actually I set the url as an arguement in a function,
// but for demonstration it will be a variable
var url = '/myPathname';
// Much later...
url = new URL (url);
However, this was returning a syntax error. Once I looked a docs, I found out why.
Per the docs, the syntax for a new URL looks like this:
url = new URL(urlString, [baseURLstring])
url = new URL(urlString, baseURLobject)
The docs also say:
baseURLstring: is a DOMString representing the base URL to use in case urlString is a relative URL. If not specified, and no baseURLobject is passed in parameters, it default to 'about:blank'. If it is an invalid absolute URL, the constructor will raise a DOMException of type SYNTAX_ERROR
A couple of examples in the docs for a baseURLstring is:
var a = new URL("/", "https://developer.mozilla.org"); // Creates a URL pointing to 'https://developer.mozilla.org/'
var b = new URL("https://developer.mozilla.org"); // Creates a URL pointing to 'https://developer.mozilla.org/'
var c = new URL('en-US/docs', b); // Creates a URL pointing to 'https://developer.mozilla.org/en-US/docs'
Thus, I am trying to figure out how to emulate a baseURLstring for, currently, localhost and eventually when this gets hosted by the main server I will use for my network, the baseURLstring for that. I'm guessing it would involve in some way getting the IP address of the computer I have/of the server on the network, or maybe not...
you can test this
var base_url = location.protocol + '//' + location.host + '/';
baseURLstring will the url of your website, lets take the example of Google:
base url of google is https://www.google.com similarly your baseurlstring will be something like this https://www.yourwebsiteaddress.com and the first parameter in url = new URL(urlString, [baseURLstring]) is the path of the files placed on your server (root folder, where your default index file is placed)

enforce http with JavaScript

I have a srcURL variable which gets a path of the form /myFolder/myFile.jpg
Now this gets assigned to the img element..which obviously would call it with the complete path https://mySite.com/myFolder/myFile.jpg
Now I somehow want the https to be replaced/enforced with http using Javascript..
I am not sure if I can do this with the "replace()" method since I only get the path "/myFolder/myFile.jpg" in the srcURL variable and not with https..
How can I do that?
You are using a relative path. You need to use an explicit path when setting the src of the URL.
srcURL = '/myFolder/myFile.jpg';
srcURL = 'http://' + window.location.host + srcURL;
// srcURL == 'http://<yourdomainname>/myFolder/myFile.jpg'
Note: you'll probably get a warning message saying some parts of your page may be unsecure.
If you want to enforce plain HTTP, you should write a rewrite rule on the server to forward any HTTPS request for an image to the HTTP equivalent. On the client side, simply doing this would be sufficient (but you really need the back end piece too):
url.replace("https", "http");

How do I get the referrer's domain/host name using JavaScript?

I know I can get the host name of the current page, by simply doing:
var myhostname = location.hostname;
But how do I get the host name of the referrer? I can get the referrer by
var referrer = document.referrer;
but unfortunately there's no document.referrer.hostname available in JavaScript. How can I get this value?
An example of where this is useful is if somebody clicks a link on google.com. I want to be able to retrieve google.com from the referrer (not the page and the query string).
This would do:
document.referrer.split('/')[2];
Example.
function parseURL(url) {
var a=document.createElement('a');
a.href=url;
return a.hostname;
}
This is a relatively old question, nevertheless this may help any followers.
By parsing it. document.referrer.split( '/' ); will get you close. Or take a look at this
http://blog.stevenlevithan.com/archives/parseuri
If referrer is coming from a browser, it will be sane -- but just in case you want more robust parsing.
You can use var referrer = new URL(document.referrer).hostname.
See https://developer.mozilla.org/en-US/docs/Web/API/URL.URL.
You can use regexp to extract this data.
string.match(/^http([s]?)://([a-zA-Z0-9-_\.]+)(:[0-9]+)?/);
Hi use this function to get domain name.
function getDomain(url) {
if (url) {
var match = /(?:https?:\/\/)?(?:\w+:\/)?[^:?#\/\s]*?([^.\s]+\.(?:[a-z]{2,}|co\.uk|org\.uk|ac\.uk|org\.au|com\.au))(?:[:?#\/]|$)/gi
.exec(url);
return match ? match[1] : null;
} else
return null;
}
It includes the protocol, but document.origin will work. It works via the Origin header, which has no path information included with it.

Get the current url but without the http:// part bookmarklet!

Guys I have a question, hoping you can help me out with this one. I have a bookmarklet;
javascript:q=(document.location.href);void(open('http://other.example.com/search.php?search='+location.href,'_self ','resizable,location,menubar,toolbar,scrollbars,status'));
which takes URL of the current webpage and search for it in another website. When I use this bookmarklet it takes the whole URL including http:// and searches for it. But now I would like to change this bookmarklet so it will take only the www.example.com or just example.com (without http://) and search for this url. Is it possible to do this and can you please help me with this one?
Thank you!
JavaScript can access the current URL in parts. For this URL:
http://css-tricks.com/example/index.html
window.location.protocol = "http"
window.location.host = "css-tricks.com"
window.location.pathname = "/example/index.html"
please check: http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
This should do it
location.href.replace(/https?:\/\//i, "")
Use document.location.host instead of document.location.href. That contains only the host name and not the full URL.
Use the URL api
A modern way to get a part of the URL can be to make a URL object from the url that you are given.
const { hostname } = new URL('https://www.some-site.com/test'); // www.some-site.com
You can of course just pass window location or any other url as an argument to the URL constructor.
Like this
const { hostname } = new URL(document.location.href);
Do you have control over website.com other.example.com? This should probably be done on the server side.
In which case:
preg_replace("/^https?:\/\/(.+)$/i","\\1", $url);
should work. Or, you could use str_replace(...), but be aware that that might strip 'http://' from somewhere inside the URL:
str_replace(array('http://','https://'), '', $url);
EDIT: or, if you just want the host name, you could try parse_url(...)?
Using javascript replace via regex matching:
javascript:q=(document.location.href.replace(/(https?|file):\/\//,''));void(open('http://website.com/search.php?search='+q,'_self ','resizable,location,menubar,toolbar,scrollbars,status'));
Replace (https?|file) with your choice, e.g. ftp, gopher, telnet etc.

Categories

Resources