How to work with url in JavaScript?? | Sololearn: Learn to code for FREE!

+6

How to work with url in JavaScript??

Suppose i have a website https://www.example.com No i want if if any country name is add after this url show the details of that country Example : https://www.example.com/India Details --> .... And if it is a wrong country name show a custom error page (If there is any youtube video related to this please share the link) This is only a example i want to do many things using URL JavaScript

11/25/2021 2:01:59 PM

Time To Code

10 Answers

New Answer

+4

Time To Code oh you mean search paramaters but by your questions it seemd like you are trying to do something with url paths i myself had used search paramaters in my sololearn code previewer its machemism is simple just look for the specific params when page loads and according to that you can perform actions here's an article by Google devs https://developers.google.com/web/updates/2016/01/urlsearchparams

+6

it seems you trying to build an api you may use nodejs for that this may help you https://medium.com/@onejohi/building-a-simple-rest-api-with-nodejs-and-express-da6273ed7ca9 u can use url params to get the name of the country

+4

Beauty A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Arnav Kumar

+3

Arnav Kumar and Beauty Not exactly Api i want to work with a Website url For example https://www.instagram.com/Mama This will show u mama details And if this is not found show a custom error page coz api will show u a details but i want to show a website page

+3

yes Time To Code you can do that with nodejs read the article you'll get it instead of json data you can just return an html page

+2

Time To Code I think PHP and Node.js can help you a lot

+2

It means this all is done using node.js I have seen many websites playing with their url window.location.search IndexOf("?") .... Why do they do so ?? 🤔

+2

To handle urls and show a 404 for exmple, you need a javascript URL router. There are many frameworks and libraries which you can use. Like: react, angular, vue, etc. Or you can install url-router using npm: url-router -npm. If you don't want to use any of those, then you can create your own url router. or you can build something simple like this: const pages =["india","germany","belgium","albania",....]; then use: const page = pages.find(c=> window.location.protocol +"/"+window.location.host+window.location.pathname.lastIndexOf(c)>-1); if(page == undefined){ //REDIRECT TO PAGE NOT FOUND } I would still advice to use a router framework or library for js.

0

Location.Search is used to search a part of the url. IndexOf can be used as well to look for 1 to many chars in a string.

-3

add tag img in the html.