I am building a javascript bookmarklet which can change some part of url and can open updated URL. Below is the code I have written.
var str = "www.myweb.com/in/products/index.aspx";
var pattern2 = new RegExp('www.myweb.com','i');
var str1 = str.replace(pattern2, 'https://www-stg.myweb.com:60002');
window.location.href = str1;
This is resulting in http//www-stg.myweb.com:60002/in/products/index.aspx which in incorrect. I want to add https:// before www-stg.myweb.com
If I alert it or console.log() it, it will show correct thing. But browser is adding http once submitted.
How to overcome this?
I suggest to use an anchor element for such operations. No need for complicated expressions.
var str = '//www.myweb.com/in/products/index.aspx';
var a = document.createElement('a');
a.href = str;
a.protocol = 'https';
a.host = 'www-stg.myweb.com:60002';
console.log('result', a.href);
Fiddle
Make sure str begins with // or http://, otherwise the current hostname will be used.
Related
I'm a newbie in web development so pls forgive my newbie question.
I have a URL "https://123asd.my.website.com/blabla/blabla/blabla
What I'm trying to figure out is how do I get the "123asd" so that I can set in on my var. Thank you
You can use regex
var url = 'https://123asd.my.website.com/blabla/blabla/blabla';
var number = url.match(/([0-9a-z]{1,})\./)[1];
console.log(number);
const url = "https://123asd.my.website.com/blabla/blabla/blabla";
let firstStr = url.replace("https://", ""); // get rid of "https://" or you can do it by some other way
firstStr = firstStr.substring(0, firstStr.indexOf('.')); // get the substring start from the beginning to the first '.'
console.log(firstStr); // 123asd
var url="https://123asd.my.website.com/blabla/blabla/blabla";
var urlNoHttps=url.replace(/^https?\:\/\//i, "");
var hostName=urlNoHttps.split('.')[0];
console.log(hostName);
The above code works for both http and https protocol.
I'm trying to make a bookmarklet that will take part of an URL and redirect to the new URL, but I need to change two parts of the URL that are separate.
The base URL could be:
78.media.tumblr.com/fc87fac5ea0d88e1e22a214d25a169ee/tumblr_p3fjmdiF7f1r9qk1io1_1280.png
I need it to end like this:
s3.amazonaws.com/data.tumblr.com/fc87fac5ea0d88e1e22a214d25a169ee/tumblr_p3fjmdiF7f1r9qk1io1_raw.png
So I need to replace "78.media.tumblr.com" and "1280"
I've tried coming up with something using window.location.assign and location.href.replace but I'm pretty new and couldn't figure it out.
You can do this with regex and window.location.href. This is assuming you are only looking at tumbler though. If you're not, there would be another step in the regex.
// first get the url
var url = window.location.href;
// Use regex to keep only the parts we want and replace the others
var newUrl = url.replace(/.*(\.tumblr.*\_).*(\..*)/, 'http://s3.amazonaws.com/data$1raw$2')
// go to the new page
window.location.href = newUrl;
In general, you can just replace the parts of the string using String.prototype.replace. Depending on how flexible you need the matching to be you can adjust the regexes to be more or less 'matchy'.
const startUrl = '78.media.tumblr.com/fc87fac5ea0d88e1e22a214d25a169ee/tumblr_p3fjmdiF7f1r9qk1io1_1280.png'
const endUrl = 's3.amazonaws.com/data.tumblr.com/fc87fac5ea0d88e1e22a214d25a169ee/tumblr_p3fjmdiF7f1r9qk1io1_raw.png'
const tumblerRegex = /.*\.tumblr\.com/
const numberRegex = /_\d{4}/
function transform (start) {
return start.replace(tumblerRegex, 's3.amazonaws.com/data.tumblr.com').replace(numberRegex, '_raw')
}
console.log(transform(startUrl) == endUrl)
I have no idea what I'm doing wrong here. I'm trying to encode my URL with javascript. But the URL never gets put into the tweet. I think it has something to do with some parameters in my URL having spaces.
https://twitter.com/intent/tweet?text=40%25%20Off%20Prom%20Tuxedo%20Rental&url=http%3A%2F%2Fexample.com%3A5757%2Fcoupon%3Fref_name%3DTest%20Name%26school%3DTest%20School
If I take out the %20's from my URL then it works...
https://twitter.com/intent/tweet?text=40%25%20Off%20Prom%20Tuxedo%20Rental&url=http%3A%2F%2Fexample.com%3A5757%2Fcoupon%3Fref_name%3DTestName%26school%3DTestSchool
But I need to keep those spaces in there.
This is the javascript code I have right now...
var text = encodeURIComponent("40% Off Prom Tuxedo Rental");
var couponURL = encodeURIComponent("http://example.com/coupon/?ref_name=Test Name&school=Test School");
var twitterURL = "https://twitter.com/intent/tweet?text=";
var twitterURL = twitterURL+text+"&url="+couponURL;
In this situation, a percentage sign equals %25, so if you want to include the spaces in your tweet link, use %2520 instead of %20. So wherever you were going to use %20, use %2520 instead.
var text = "40% Off Prom Tuxedo Rental";
text = text.replace(/\s/g, "%2520")
I have an URL e.g http://test.example.com I need to extract test (sub domain of this url.) from this url. This url is dynamic it may change so couldn't match with test and extract it. I have written block of code it is working for me. Can anyone suggest me better way to achieve this.
var siteUrl = 'http://test.example.com';
var parts = siteUrl.split('.');
var subdomainstr = parts.shift(); // Output 'http://test'
var upperleveldomain = parts.join('.'); // Output 'example.com'
var extractSubDomain = subdomainstr.split('//');
var subdomain = extractSubDomain.slice(1).join('.');
console.log(subdomain); //Output test
fiddle
Why not this?
var subdomain = siteUrl.split('//')[1].split('.')[0];
Working demo
If you prefer regex, this works
var subdomain = siteUrl.match(/http:\/\/([^\.]*)/i)[1];
Is it possible to create a new Location object in javascript? I have a url as a string and I would like to leverage what javascript already provides to gain access to the different parts of it.
Here's an example of what I'm talking about (I know this doesn't work):
var url = new window.location("http://www.example.com/some/path?name=value#anchor");
var protocol = url.protocol;
var hash = url.hash;
// etc etc
Is anything like this possible or would I essentially have to create this object myself?
Well, you could use an anchor element to extract the url parts, for example:
var url = document.createElement('a');
url.href = "http://www.example.com/some/path?name=value#anchor";
var protocol = url.protocol;
var hash = url.hash;
alert('protocol: ' + protocol);
alert('hash: ' + hash);
It works on all modern browsers and even on IE 5.5+.
Check an example here.
How about use the standard URL object?
const url = new URL("http://www.example.com/some/path?name=value#anchor");
const { hash } = url;
Then console.log(hash) will output #anchor.
Warning: This interface is a bit new, so, if you're not using a transpiler, please, check the compatibility table and do your tests at target browsers.
You can leverage the power of an anchor element
var aLink = document.createElement("a");
aLink.href="http://www.example.com/foo/bar.html?q=123#asdf";
alert(aLink.pathname);
You can parse it in a regex to get the parts as matches... I don't have the full code right now, but this can be used to get the querydata:
var myUrl = window.location.href;
var matches = myUrl.match(/([^\?]+)\?(.+)/);
var queryData = matches[2];
matches[0] is the full string, matches(1) is the first part of the URL (up to the ?)... you could build up a regular expression to parse each part of a string url if you want...
You can also use one of the many libraries already out there for this.