URL with URL parameters gets concatenated in browsers - javascript

I have the following code:
const url = 'https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=' +
encodeURIComponent('https://dev.mysite.com/google_oauth2/') +
'&scope=https://www.googleapis.com/auth/drive.file&client_id=myclientid'
window.open(url, "", "width=700,height=500")
And the super-strange behaviour is that if I clear the browser history and run this code for the first time, it works fine, BUT THEN if I run for the second and subsequent times, the urls gets concatenated and I see
https://dev.mysite.com/google_oauth2/?code=mycode&scope=https://www.googleapis.com/auth/drive.file
instead of
https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https://dev.mysite.com%2Fgoogle_oauth2%&scope=https://www.googleapis.com/auth/drive.file&client_id=myclientid
Does anyone know why this happens ?
BTW, I am using React.JS, but I don't think it has something to do with this...
Resolved. Was a Google-specific issue

I would suggest you use a more appropriate URL and URLSearchParams for such purpose: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
and https://developer.mozilla.org/en-US/docs/Web/API/URL
So your code would be:
const url = new URL('https://accounts.google.com/o/oauth2/v2/auth')
url.searchParams.append('redirect_uri', 'https://dev.mysite.com/google_oauth2/');
url.searchParams.append('scope', 'https://www.googleapis.com/auth/drive.file');
url.searchParams.append('client_id', 'myclientid');
And url.href would be:
https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https%3A%2F%2Fdev.mysite.com%2Fgoogle_oauth2%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.file&client_id=myclientid

Related

window.location.href.replace does not replace the content of the url

I would like to redirect my current page to a page with similar URL except for one parameter.
Here is what I have tried
window.location = window.location.href.replace("trip-start="+/^\d{4}\-\d{2}\-\d{2}$/g, "trip-start="+this.value);
and also:
window.location.search = window.location.search.replace("trip-start="+/^\d{4}\-\d{2}\-\d{2}$/g, "trip-start="+this.value);
Unfortunately, the page is "redirected" (i.e refreshed) but the url stays exactly the same.
Am I missing something ?
Thanks you
[EDIT]
I investigated a bit, and actually the problem amount to this
let text = window.location.href;
let result = text.replace("trip-start="+/^\d{4}\-\d{2}\-\d{2}$/g, "trip-start=2000-00-00");
alert(result);
the "result" is supposed to be the URL with the parameter "trip-start" set to 2000-00-00, but again, nothing changes.
Please use history API
history.replace(newUrl)

request.getParameter returns dirty string

I have a site which is made by a CMS. When the site is visited as normal and a user search on the site with the URL:
https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV,coursesSV,lifeLongLearningCoursesSV&showbutton=false
The JS code:
let test = requester.getParameter("identity")
console.log(test)
// programSV,coursesSV,lifeLongLearningCoursesSV
returns programSV,coursesSV,lifeLongLearningCoursesSV as expected.
But when I visit the site at the exact same URL but I come from an external page JS code above returns this instead: programSV%2CcoursesSV%2ClifeLongLearningCoursesSV
Any suggestions on what could be wrong and how this could be fixed?
Use a URL search params for consistency - it will decode the URL for you regardless of encoded entities
Alternatively use decodeURIComponent
const url1 = new URL(`https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV,coursesSV,lifeLongLearningCoursesSV&showbutton=false`)
const url2 = new URL(`https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV%2CcoursesSV%2ClifeLongLearningCoursesSV&showbutton=false`)
console.log(url1.searchParams.get("identity"))
console.log(url2.searchParams.get("identity"))
// alternative
console.log(decodeURIComponent(`programSV%2CcoursesSV%2ClifeLongLearningCoursesSV`))
NOTE: decodeURI does nothing for your commas:
decodeURI(requester.getParameter("identity"))
returns programSV%2CcoursesSV%2ClifeLongLearningCoursesSV
however decodeURIComponent does work:
decodeURIComponent(requester.getParameter("identity"))
returns programSV,coursesSV,lifeLongLearningCoursesSV
the commas are being encoded, you have to use decodeURIComponent. Like this: decodeURIComponent(requester.getParameter("identity")). See link for reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent

How to get param values from deeplinks

I open my app with a deeplink
myscheme://?param1=value1&param2=value2
How can I get the value of the parameters? I found different posts that treat this subject but the once I tried works on http links I think, I alwayse get a warning telling me that BlobURL object is not supported yet.
var url = new URL(data);
alert(url.searchParams.get("param1"));
I have tried your example, and it works, with slight adjustment:
let myscheme = 'http://www.example.com/?param1=value1&param2=value2'
var url = new URL(myscheme);
alert(url.searchParams.get("param1"));
More details can be found here.

Remove parameter from url not working

I'm attempting to remove a url parameter status from the url but in the following alert, the parameter is still there.
var addressurl = location.href.replace(separator + "status=([^&]$|[^&]*)/i", "");
alert(addressurl);
location.href= addressurl;
How do i solve?
You are confusing regex with strings.
It should be:
var addressurl = location.href.replace(separator, '').replace(/status=([^&]$|[^&]*)/i", "");
Javascript context in web pages are to the page you are working on.
When you reload, redirect or move to any other page, javascript changes done in previous page will not be there. This has to be handled from server side.
Refresh repeats the last request to the server, which is going to ignore your javascript changes. Instead navigate to the new url with window.location = addressurl;

document.location does not change the webpage in IE9?

I am trying to redirect to a different page in IE9 (9.0.3).
When I try to get/set document.location, or document.location.href, or window.location/window.location.href, I'm unable to do so. It fails without giving any errors.
I've tried to check whether the document and windows objects are set, and they are, so I have no idea why the location object is "missing".
I tried getting the document.URL and that works fine, but it's read-only.
Anyone know what the problem is or how to achieve this in a cross-browser way?
I was also experiencing the same problem but found that adding
window.event.returnValue = false;
above line in the javascript before the redirection resolved the problem.
See this: http://social.msdn.microsoft.com/Forums/en/iewebdevelopment/thread/c864ae63-66f6-4656-bcae-86b0018d70c9
Apparently it's a caching bug, you can solve it by appending a timestamp to the destination URL (that is, using a "unique" URL every time).
Perhaps your IE9 has some security restrictions in place that prevent JavaScript from directing URL's. window.location.href = "" should work normally on IE9.
Cache may be the reason, try:
location.href='something.php?tmp=' + Date.parse(new Date())
Hope it helps
You should use an absolute URL:
var url = '/section/page/';
var host = window.location.hostname;
window.location = 'http://' + host + url;
Where url is the relative path to your page.

Categories