How to obtain one part of the url using javascript only - javascript

This is about a password reset link sent to the email from firebase. I want to get the oobCode from it. But when I obtain the url of the page , it gives me the local address not the actual link I clicked in my email.Please help
https://example.com/usermgmt?mode=resetPassword&oobCode=ABC123&apiKey=AIzaSy...&lang=fr
This link actually loads the reset_pwd.html in my website. So when I try to get window.location.href , it doesnt give me the actual address displayed in the address bar.
This is what I have tried so far. But I realised I' m trying to split the local address of the web page http://127.0.0.1:80/reset_username
function func() {
var s = window.location.href;
ResponseURL = window.location.href;
var domain = ResponseURL.split('=');
alert(s);;
}

You can get it using URL.searchParams.get():
function getUrlParam(key, urlString = window.location.href) {
let url2 = new URL(urlString);
return url2.searchParams.get(key);
}
const url = `https://example.com/usermgmt?mode=resetPassword&oobCode=ABC123&apiKey=AIzaSy&lang=fr`;
console.log(getUrlParam('oobCode', url));
// Can be used without providing the URL, like:
// getUrlParam('oobCode');

Related

Adding parameters to url on page load only if no paremeters already exist

I searched for the answer to my question and even tried some solutions, but wasn't able to get anything to really work. I'm newish to javascript, so that might also be why.
I have a specific URL and whenever someone goes to that URL, I want to add parameters to it, but only if no parameters are already present. Parameters get added to the URL for other on click events, but on page load, I need a set of parameters added to the URL.
I tried to use the history API and I think I'm kind of close, but I'm not able to get it to do what I want it to do.
function addDefaultParam(url) {
var currentURL = window.location.href; //get the current url
var baseURL = '/our-partners'; //this is the url that should have params added
var paramString = '?asc=true&sortBy=display_name'; //here are the params
if (currentURL === baseURL) {
window.history.pushState("object or string", "Title", "/" + paramString);
}
return url;
}
I'm using basic js in this because that's what was used in the other functions (I inherited this code). Any help would be greatly appreciated.
You can register the addDefaultParam function to fire when the document first loads in the browser and use the Location interface to check the state of the current path and query string of the URL and if they match your conditions, update the current query string value.
See below for an example:
window.addEventListener("load", addDefaultParam);
function addDefaultParam() {
let currentPath = document.location.pathname;
let currentQueryString = document.location.search;
let targetPath = "/our-partners";
if (currentPath === targetPath && !currentQueryString) {
document.location.search = "?asc=true&sortBy=display_name";
}
}

Retrieve URL of AppScript using Javascript

I am trying to reload the current page with a new parameter, but I am unable to retrieve the correct URL of the webpage. I am getting an alternate URL instead of the one that triggers the web app
https://script.google.com/a/user/macros/s/A...ljhGC/dev?action=menu&id=1
when the button is clicked I want the action parameter's value to change and the page to reload as
https://script.google.com/a/user/macros/s/A...ljhGC/dev?action=checkout&id=1
Here is my javascript within the HTML File
console.log(document.url);
var url = new URL(window.location.href);
var query_string = url.search;
var search_params = new URLSearchParams(query_string);
search_params.set('action', 'checkout');
url.search = search_params.toString();
var new_url = url.toString();
window.location.replace(new_url);
console.log(new_url);
}
This is the URL that gets logged
https://n-ikwx...khq-1lu-script.googleusercontent.com/userCodeAppPanel?action=checkout
How do I retrieve the actual URL that is in the address bar?
Thanks in advance!
Issue:
A iframe with different origin cannot read the href property of location of the the parent/top frame, which is write only. So, You can't.
Solution:
You can however pass the url from server side.
Server side:
function getTopUrl(){
return ScriptApp.getService().getUrl();
}
Client side:
var topUrl;
function getTop(){
google.script.run.withSuccessHandler((url)=>{topUrl=url;}).getTopUrl();
}
window.addEventListener('load', getTop);
References:
Where is my iframe in the published web application/sidebar?
Same origin policy
Service#getUrl
google.script#run

Change id value on external page after win.open

I am new to js and have been forcing myself to do most of my front end work with it as I feel it is the best learning experience for me at my job.
In a nutshell, I am trying to take the current page url and paste it into the 'title' text-area on the new page.
Basic idea: Open new tab after clicking a button.
var curUrl = window.location.href;
var link = 'www.issue.com/'
var button = $('#my-button');
button.on('click', function() {
window.open(link, '_blank');
});
After it opens the desired site, I'd like to paste the url from the previous site inside of the new sites input.
$(document).on('load', function() {
$('#title-box').val(url);
});
I have spent a few hours trying multiple things that I have found in my time researching. Any help would be greatly appreciated!
You can get URL using JS by;
var url = window.location.href;
Now there are two ways to achieve what you want;
1. Store data in local storage/ session storage
localStorage.setItem("prevurl", url); //OR sessionStorage.setItem("prevurl", url);
Now proceed as per your code in newly opened page;
$(document).on('load', function() {
var prevUrl = localStorage.getItem("prevurl"); //OR sessionStorage.getItem("prevurl");
$('#title-box').val(prevUrl);
});
2. Second option is like below, i.e., send previous url via get parameter
In previous page;
$("button").click(function(){
window.open("newpageurl?ref=" + window.location.href);
})
In new page;
var curUrl = window.location.href;
var ref = getRefFromUrl(curUrl); //implement getRefFromUrl function yourself
And finally;
$('#title-box').val(ref);

how to get the full path of a page in javascript

I know that var pathname = window.location.pathname; returns path only and var url = window.location.href; returns full URL.
Now suppose i have page MyPageName.aspx in the root of my site and my site can be deployed on servers serverone, servertwo & serverthree.
On Serverone, i want to display http://example.com/MyPageName.aspx
On servertwo, i want to display http://example.net/MyPageName.aspx
On serverthree, i want to display http://example.org/MyPageName.aspx
So how do i get the full URL path of a page in i'ts current environment with out browising to that page but knowing in advance that the page exists.
I want to display the URL some where on a master page.
You can use window.location.origin to return everything up to and including the .com:
var url = window.location.origin;
-> "http://example.com"
As MyPageName.aspx appears to be static, you can then just use string concatenation to append it to the end:
url += "/MyPageName.aspx";
-> "http://example.com/MyPageName.aspx"
Demo
var url = window.location.origin + "/MyPageName.aspx";
document.write(url);
Did you try
document.URL
read http://www.w3schools.com/jsref/prop_doc_url.asp

Javascript passing an encoded url to window.location.href

I'd like to redirect a page with javascript using the following code:
var s = 'http://blahblah/' + encodeURIComponent(something);
alert(s);
window.location.href = s;
The alert shows the correct encoded url but when I pass it to window.locaion.href, it redirects the page to the unencoded url which is wrong.
How could I do it properly?
Thanks
This could be related to (a) using firefox or (b) specific APIs that you're feeding encodedComponent into, like Google search.
Here's one tested solution on Firefox-stable:
var clearComponent = 'flowers for my boyfriend & husband on valentines';
var encodedComponent = encodeURIComponent(clearComponent);
var googleSafeComponent = encodedComponent.replace(/%20/g,'+'); // replaces spaces with plus signs for Google and similar APIs
var completeURI = 'http://google.com/?q=' + googleSafeComponent;
window.location = completeURI;
Or all in one line:
window.location = 'http://google.com/?q=' + encodeURIComponent('flowers for my boyfriend & husband on valentines').replace(/%20/g,'+');
window.location implies window.location.href so you can save some letters. ;)

Categories

Resources