Yuuu
Yuuu

Reputation: 174

How to get a part of a url string with pure javascript

I have this url, for example:

https://www.google.com/calendar/render#main_7%7Cmonth

I need to get ONLY calendar with or witout /

Why in pure JavaScript? I want to put it in a bookmark, so i cant use any kind of JS.Framework...

Some of you could say that post is duplicated. Thats why you dont use the search-tool nor read the entire question. Thanks for other who really read everything.

PD:Yeah, i've readed this solution.. this does not fit me. JavaScript - Get Portion of URL Path

Upvotes: 0

Views: 276

Answers (5)

user2182349
user2182349

Reputation: 9782

Search for calendar and extract the remainder of the string -

var s = 'https://www.google.com/calendar/render#main_7%7Cmonth';
var o = s.indexOf('calendar');
if (o !== -1) {
    // Found calendar at offset o
    console.log(o);
    console.log(s.substr(o));
}

Upvotes: 0

Nikola
Nikola

Reputation: 101

var url = "https://www.google.com/calendar/render#main_7%7Cmonth";
var split_url = url.split('/');
for(var i=0; i < split_url.length; i++)
  if(split_url[i] == 'calendar')
    alert("It's a calendar");

Upvotes: 0

Gleb
Gleb

Reputation: 1332

Use split function to divide string to array:

"https://www.google.com/calendar/render#main_7%7Cmonth".split('/')[3];

Upvotes: 1

ssbb
ssbb

Reputation: 1931

(I'm a bit sad answering this).

var whatyouwant = url.split('/')[3];

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

Assuming calendar is always on the same position you could do

var url = 'https://www.google.com/calendar/render#main_7%7Cmonth';
var calendar = url.split('/')[3];

Upvotes: 1

Related Questions