how do I create a page name from the location.pathname url? - javascript

I have the following url :
http://localhost/get/this/url/in/a-nice-format.html
var pagename= location.pathname.split('/').join(':');
gives me
:get:this:url:in:a-nice-format.html
How do I get rid of the first ":"?
Thanks.

pagename = location.pathname.split('/').join(':').substr(1);

Hi you can use substrings method. Simply do this....
var pageName2 = pageName.substring(1); I hope this helps.

I'm not able to comment yet but waned to help anyway. And there's probably an answer somewhere else...
Anyway, try using slice() and passing in a 'begin' argument of 1 to remove the first ':'.
Like this: var pagename= location.pathname.split('/').join(':').slice(1);

Related

use javascript split function twice

I have the following string:
12.1.20.1, 81.32.68.68:50321
however I need to make it like this:
81.32.68.68
Could someone help me out with this?
I've already tried to use split(",")[1] but I also need to remove the :50321
Thank you!
If you can use the split function twice on it, you can do something like this:
console.log(
`12.1.20.1, 81.32.68.68:50321`
.split(", ")[1]
.split(":")[0]
);
This gives just the IP address out.
81.32.68.68

Getting query string from re written URL

I have url "SampleProject/profile/aA12". How can I get the value of the id from my rewritten URL using javascript? I want to get the "aA12" value.
Im using htaccess rewrite to rewrite my URL. Im new in rewritting url's. Any help will be appreciated. More powers and thank you.
You can use regex.
Try
'SampleProject/profile/aA12'.match(/\SampleProject\/profile\/(\w+)/)
'SampleProject/profile/aA12/xxx'.match(/\SampleProject\/profile\/(\w+)/)
'aA12' will be matched in both cases.
There are going to be quite a few ways to achieve your goal with JavaScript. A simple solution could be something like this:
let myURL = "SampleProject/profile/aA12";
let result = myURL.split('/').pop();
// returns "aA12"
The .split('/') method is dividing your string up into an array using the / character, and .pop() is simply returning the last element of that array.
Hope this helps! If you were looking for more advanced matching, i.e. if you wanted to ignore a potential query string on the end of the URL parameter, you could use regular expressions.
Their is a many way that you can use to achieve the desired method i made you a code pen in this link
var url = "SampleProject/profile/aA12";
let res = url.split('/').pop();
console.log(res)
https://codepen.io/anon/pen/KQxNja

JavaScript, get url body in a variable

I know it is an easy one, sorry for asking, but I’m going mad and I cannot get it. Must be the fundamentals, I’ve got them all mix up :((
I have this url:
https://www.myweb.sub.com/~user/folder/file.php?param=Value
I need to get:
https://www.myweb.sub.com/~user/folder/
in a variable.
Please help to achieve it.
Thanks a lot
You can use the slice() and lastIndexOf() methods:
var url = "https://www.myweb.sub.com/~user/folder/file.php?param=Value";
var segment = url.slice(0, url.lastIndexOf("/") + 1);
If your current URL is actually https://www.myweb.sub.com/~user/folder/file.php?param=Value, operate on window.location.href instead.
So we don't chew it all up for you, here's a link that should set you on the right path:
find the last index of a character with this: http://www.w3schools.com/jsref/jsref_lastindexof.asp
get the substring of a string with this:
http://www.w3schools.com/jsref/jsref_substring.asp
Tell us if you still need help after that.
use this :-
var str="https://www.myweb.sub.com/~user/folder/file.php?param=Value";
var n=str.substring(0,str.indexOf("file"));

Javascript & HTML Getting the Current URL

Can anyone help me. I don't use Client-side Javascript often with HTML.
I would like to grab the current url (but only a specific directory) and place the results between a link.
So if the url is /fare/pass/index.html
I want the HTML to be pass
This is a quick and dirty way to do that:
//splits the document.location.href property into an array
var loc_array=document.location.href.split('/');
//have firebug? try a console.log(loc_array);
//this selects the next-to-last member of the array.
var directory=loc[loc.length-2]
url = window.location.href // Not particularly necessary, but may help your readability
url.match('/fare/(.*)/index.html')[1] // would return "pass"
There may be an easier answer, but the simplest thing I can think of is just to get the current URL with window.location and use some type of parsing to get which directory you are looking for.
Then, you can dynamically append the HTML to your page.
This may get you started:
var linkElement = document.getElementById("whatever");
linkElement.innerHTML = document.URL.replace(/^(?:https?:\/\/.*?)?\/.*?\/(.*?)\/.*?$/i,"$1");

How to Extract the Current URL and ID in jQuery?

How would I go about getting the current URL using jquery, or more specifically, getting an ID on the end of it?
For example, I have product.php#tab-2. What I want to get from it is just the '#tab-2' part.
I have tried 'window.location.pathname' but that will only return '/product.php'
Thanks
You don't need jQuery for this:
alert(window.location.href); // will give you the full url
alert(window.location.hash); // will give you the hash (#) value
See the Mozilla docs at window.location - MDC.
You want window.location.hash
Use jqUrl plugin (http://www.oakcitygraphics.com/jquery/jqURL/jqURLdemo.html) to retrieve the full current url, then strip the window.location.pathname part.
Thomas
To actually assign to a variable, use the following;
var url = window.location.href;
var id = url.substring(url.lastIndexOf('#') + 1);
Note that if your url is in this format /product.php/3 , then you can use the above code, just change the character is the lastIndexOf function.

Categories