in Education by
I wanted to find a way to extract from the url the classes to be added to the body. I made some attempts but I only managed to get the last piece of the url. Do you know how I could do? this is what I tried to do with jquery var dir= window.location.pathname.split('?')[0].split('/').filter(function (i) { return i !== ""}).slice(-1)[0] jQuery("body").addClass(dir); this is what i have: http://www.test.com/news/hello/ http://www.test.com/news/ this is what I would like to have http://www.test.com/news/hello/ JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The slice method return an array so all you need to do is removing the [0] so that you can have all your classes. You should also put 2 as the paramater of slice so that it can take all your classes after your base url. var url = " http://www.test.com/news/hello/" var dir = url .split('?')[0] .split('/') .filter(function (i) { return i !== ""}) .slice(2); $("div p").addClass(dir); .news { color: red; } .hello { font-size: 20px; }

Hello world

Run code snippetExpand snippet

Related questions

0 votes
    I wanted to find a way to extract from the url the classes to be added to the body. I made some ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have some divs like: next and the script in another page link.htm: Slide I want to load link. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 28, 2022 in Education by JackTerrance
0 votes
    If my post duplicated, please give me references. because i failed finding post that appropriate from my ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am using jquery ajax get. SO when my form is submitted it then submits to a php file but the ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    I am using jquery ajax get. SO when my form is submitted it then submits to a php file but the ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I have the dataset that has the no_employees column that is the str object. whats is a best way to create the new ... 1-5 |Very Small Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    When two classes can be separated by a separate line, they are known as? (a) linearly separable (b) linearly ... (d) none of the mentioned Please answer the above question....
asked Sep 3, 2022 in Education by JackTerrance
0 votes
    I have the following Javascript code which controls an accordion type set of divs. The set of divs is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    When I use ajax call from a modal window, the request url seems not correct. I have a webserver ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I need to work with an ajax powered web page so I can pass an id or ids in the URL and it ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    Each web page has its own website (URL) address.true or false Select the correct answer from above options...
asked Dec 2, 2021 in Education by JackTerrance
0 votes
    We have studied the air pollution, water pollution and soil pollution in detail in earlier classes. ... Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 8, 2021 in Education by JackTerrance
0 votes
    var EventAddress=$(".EventAddress").text(); $(".EventDirectionLink a").attr("href", url + EventAddress ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
...