I want to extract the domain with a hash without the long random id from the location address bar to make it to show like this:
http://test.example.com/#inbox
Here is the full URL:
http://test.example.com/#inbox/U2FsdGVkX19stSSdMXLZq5v7bOgzRLtaM7Lr1t+lWpI=
Here is what I have tried so far:
var url = location.hash.split('#inbox')[1];
And I have also tried this:
var url = $(location).attr("href").split('/')[2];
Unfortunately, I didn't get what I want to extract the domain and the hash without the long random id.
When I try it, it extracts the domain name and the hash without the random id to get the return output for the URL like http://test.example.com/#inbox.
I would use indexOf() to find the position of the '#' character. Since the URL can't include a '#', it is safe to assume that the first '#' will be the one you're interested in.
Then I would search for the '/', again with indexOf(), so the word between the '#' and the '/' can change and things still work as expected.
That position is the location of the '/' and anything before that can be returned which is what the String.sub() function does and it returns that URL you're looking for:
var pos = location.href.indexOf('#')
pos = location.href.indexOf('/', pos)
var url = location.href.substr(0, pos)
Another way, although I'm not sure whether it's fully portable, is to use the location fields like so:
var url = location.origin + location.pathname + "#inbox"
If the "#inbox" part can change, you may be able to tweak the code or search for a slash in location.hash like so:
var pos = location.hash.indexOf('/')
var url = location.origin + location.pathname + location.hash.sub(0, pos)
Either way should be plenty fast anyway (especially since you won't need to loop over such).
I got the whole URL, found where the "#" is, added 6 to its count for "#" + the word "inbox", and dropped the rest of the URL:
var url = document.URL;
var i = url.indexOf('#') + 6; // 6 is length of "#" + the word "inbox"
var answer = url.substr(0, i);
Try the code below:
// Complete URL
var url = 'http://test.example.com/#inbox/U2FsdGVkX19stSSdMXLZq5v7bOgzRLtaM7Lr1t+lWpI=';
var desiredResult = url.match(/.*\/\#inbox\//)[0];
console.log(desiredResult.substring(0, desiredResult.length - 1)); // this will output 'http://test.example.com/#inbox'
Related
I need to fetch the "NEED_THIS' from the below URL as separate strings.
'https://something--something.lightning.force.com/lightning/o/NEED_THIS/NEED_THIS?inContextOfRef=1.eyJ0eXBlIjoic3RhbmRhcmRfX29iamVjdFBhZ2UiLCJhdHRyaWJ1dGVzIjp7Im9iamVjdEFwaU5hbWUiOiJTdXBwb3J0X1JlcXVlc3RfX2MiLCJhY3Rpb25OYW1lIjoibGlzdCJ9LCJzdGF0ZSI6eyJmaWx0ZXJOYW1lIjoiUmVjZW50In19&count=1'
Is there any easy way to fetch this?
I already used decodeURIComponent(window.location.href.split('?')[0]); to get upto
https://something--something.lightning.force.com/lightning/o/NEED_THIS/NEED_THIS
First things first,
window.location.protocol = “https:”
window.location.host = “something--something.lightning.force.com”
window.location.pathname = “/lightning/o/NEED_THIS/NEED_THIS”
window.location.search = “?inContextOfRef=1.eyJ0eXBlIjoic3RhbmRhcmRfX29iamVjdFBhZ2UiLCJhdHRyaWJ1dGVzIjp7Im9iamVjdEFwaU5hbWUiOiJTdXBwb3J0X1JlcXVlc3RfX2MiLCJhY3Rpb25OYW1lIjoibGlzdCJ9LCJzdGF0ZSI6eyJmaWx0ZXJOYW1lIjoiUmVjZW50In19&count=1”
to break the pathname up, you can split it using:
var pathArray = window.location.pathname.split('/');
Then access the different parts by the parts of the array, like
var secondLevelLocationPartOne = pathArray[2];
var secondLevelLocationPartTwo = pathArray[3];
Then you can use it whereever you need it. for reference, click this link
We use three query strings that are pulled into form fields before a user submits. src, cst, and cid are the three parameters. cid and cst always need to be updated to the correct value. src will vary, but if none exists on the URL it should add a default one. I have this working, but think there has to be a much quicker, easier, faster way to do all of this.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
var src = "Report";
var cid = "7013475893xfvg";
var cst = "Responded";
var vsrc = "src";
var vcid = "cid";
var vcst = "cst";
var srcstring = "&" + vsrc + "=" + src;
if ('URLSearchParams' in window) {
var searchParams = new URLSearchParams(window.location.search)
searchParams.set(vcid, cid);
searchParams.set(vcst, cst);
var newRelativePathQuery = window.location.pathname + '?' + searchParams.toString();
history.pushState(null, '', newRelativePathQuery);
}
if (!(window.location.href.indexOf(vsrc) > -1)) {
window.location.search += srcstring;
}
</script>
First, the script adds or updates the cid and cst values to the correct ones defined in the variable. Then, the script determines whether "src" is present in the address bar. If it's not present, it adds the src parameter with a default value.
The goal is for this to be limited to at most one redirect, or history record. Ideally, the user will never see a reload, redirect take place and will not be present in their history. This works currently on Google Chrome desktop, but on Safari Mobile history records are created.
Edit: We discovered an issue with this code that also prevents form fields from gathering values if "%20" is any where in the URL. %20 gets converted to a "+" and breaks this process. How can we avoid this?
I need to replace the window URL with a new one based on a checkbox input. I'm trying to do this like so:
// get checkbox value
var selection = $('input[type="checkbox"]:checked').val()
// get current url
var url = window.location.href;
// prefix current URL with checkbox
url += '&site=' + selection + '&page=1';
// debug line
// console.log(url);
// navigate to new URL
location.replace(url);
I have a series of parameters at the end of my URL that looks like:
mywebsite.com&site=main&page=1
I either need to replace the entire URL, or just the parameters below:
&site=main&page=1
The key is in the last line. I've tried a few things this is the output:
Both location.assign(url); and location.replace(url); gives: mywebsite.com&site=main&page=1&site=main&page=1
window.location.href() seems to have the same behavior, it does not replace the entire URL.
What I don't understand is for location.replace() - sanity checking the behavior on reading MDN, it quotes:
The Location.replace() method replaces the current resource with the
one at the provided URL.
Ok, lets do it. Then this happens:
http://mywebsite.com&site=main&page=1&site=main&page=1
Beating to the punch - in the console log - it returns the correct URL parameters. So output in my console looks like:
http://mywebsite.com&site=main&page=1
Why is it not replacing the entire URL with the new one? How do you just replace the entire URL with a new one, or somehow just target the parameters? Regex?
Why not just build the URL you want and set that?
var url = 'http://mywebsite.com?site=' + selection + '&page=1';
location.href = url;
If you don't know what page you are on and that will be dynamic you can do:
var hostAndPath = location.host + location.pathname;
var selection = $('input[type="checkbox"]:checked').val();
var url = hostAndPath + '?site=' + selection + '&page=1';
location.href = url;
I want to get the part of a URL after the last / and before the querystring.
So far I had the last part of the URL, but I wasn't able to remove the querystring from the URL.
The Javascript:
<script type='text/javascript'>
var url = window.location.href;
var array = url.split('/');
var lastsegment = array[array.length-1];
document.write(lastsegment);
</script>
The structure of the URL is like this:
http://www.example.com/search/People?&results=latest
I only need the People part. How could that be possible within the above Javascript?
Or is there any better way to do that?
Try using window.location.pathname instead of window.location.href. It gets only the text between the server name and the query string (which is "/search/People" in your case):
var last_part = window.location.pathname.split('/').pop();
You can read more about pathname here.
Read Window.location
window.location.pathname.split('/').pop();
.pop()
your code does the right thing, you could remove query strings by amending it a bit as;
var lastsegment = array[array.length-1];
if(lastsegment.indexOf('?'))
lastsegment = lastsegment.split('?')[0];
UPDATE:
To handle the case if there are no query string embedded at all.
If you want to parse an URL from another origin than window.location :
var test = 'http://www.example.com/search/People?&results=latest';
var parts = test.split('/');
lastPart = parts[parts.length-1].split('?')[0];
lastPart is now "People"
here's the code:
function chatWin(url,name){
var nw;
var splitUrlResults = url.split("#");
url = appendDataWakeNVPs(splitUrlResults[0]) + '#' + splitUrlResults[1] ;
nw=window.open(url,name,"height=600,width=433,left=100,top=100,resizable=yes,scrollbars=no,toolbar=no,status=no");
if (nw.focus) {nw.focus();}
}
and then the link in the code:
PROD_TAB_EXPRT_LNK_EMAIL=javascript:chatWin('/customerService/contactUs/help.html#1','Help')
but the rendered code is:
<a href="javascript:chatWin('/customerService/contactUs/help.html#0#1','Help');">
Every link is getting #0 appended before the tab identifier- (#1 in this case).
thoughts?
It looks to me like the only way url ends up with #0#1 on the end of it is if appendDataWakeNVP() is appending #0 onto it's return value and your code is then adding the #1 onto the end of that.
So, I think your problem is in appendDataWakeNVP(). I'd suggest stepping into that function in your favorite debugger and you can discover what it does. Or grep for it in your source tree and examine it in your editor. If it's suppose to add #0 onto the end and you can't change that, but you don't want that there, then you will have to remove that before appending your own hash onto the end of it.
Any hash value you don't want can be removed with this:
url = url.replace(/#.*$/, "");
So, you could change this line of code:
url = appendDataWakeNVPs(splitUrlResults[0]) + '#' + splitUrlResults[1] ;
to this:
url = appendDataWakeNVPs(splitUrlResults[0]).replace(/#.*$/, "") + '#' + splitUrlResults[1] ;