0

I want to be able to take a base64 encoded url string and retrieve the name and descriptor parameter values from it in jquery.

I have it working with the parameters not base64 encoded by getting the url string as an array and then lifting out the values of the 2 parameters (name and descriptor) - but when I base64 encode the parameters in the url, I can decode the string, but it comes back as a string and not an array so I cant lift out the values of the name and descriptor parameters.

so url would be https://example.com?name=fred&descriptor=Boss and encoded to base64 https://example.com?P25hbWU9ZnJlZCZkZXNjcmlwdG9yPUJvc3M=

So from https://example.com?P25hbWU9ZnJlZCZkZXNjcmlwdG9yPUJvc3M= how can I get the values of name and descriptor in jquery and set them as variables?

Maybe its easier to not get an array from the decoded version, and just use jquery to get the values after the "+" atob decoding?

2 Answers 2

2

Another solution inspired by @Hackerman's answer with URL and URLSearchParams. Note that this solution rely on native API without any need of magic string:

const url = 'https://example.com?P25hbWU9ZnJlZCZkZXNjcmlwdG9yPUJvc3M='
const encodedUrl = new URL(url);
const decodedQuery = atob(encodedUrl.searchParams); 
const params = new URLSearchParams(decodedQuery)

// get name and descriptor
fetchedName = params.get("name");
fetchedDescriptor = params.get("descriptor");

console.log(fetchedName)
console.log(fetchedDescriptor)

// get all entries
const entries = new URLSearchParams(decodedQuery).entries()

for (const [key, value] of entries) {
  console.log(`${key}, ${value}`);
}

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this - I think your solution might work better as the other one is reliant on both parameters appearing in the url string and being set in order - can you help me with a more specific answer to my question on how this approach would work fo my two parameters name and descriptor please?
did you try this answer? It does use your two parameters name and descriptor
yes I did, but i need to be able to pass the values of the parameters to 2 different variables - but I am unsure on how to do this based on your solution. e.g. I want to get the value of name= into a jquery variable called fetchedName and the value of descriptor= into a jquery variable called fetchedDescriptor
@dubbs alright. updated.
1

You don't need JQuery to do that, you can use vanilla Javascript if you want:

let url = 'https://example.com?P25hbWU9ZnJlZCZkZXNjcmlwdG9yPUJvc3M='
let encodedUrl = url.split('?')[1]
let decoded = atob(encodedUrl)
let arrValues = decoded.split('&')
console.log('Name', arrValues[0].split('=')[1])
console.log('Description', arrValues[1].split('=')[1])

Just remember to use the atob function https://developer.mozilla.org/en-US/docs/Web/API/Window/atob

4 Comments

This only seems to get the first parameter? - dont worry, got it working actually.
Only problem I have now is that the value of the parameters if they include spaces comes through with %20
I used decodeURI to resolve this
Is there a way to adapt this so it works when only 1 of the 2 parameters is set and not reliant on them being set with name first?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.