jquery url builder/parser - javascript

I'm searching for a jquery plugin for full URL manipulation (parsing, building).
Example:
var url = 'http://mypage.com/?param=1'
var params = $.getParams(url) # {param: 1}
var newUrl = $.newUrl(url, {param:2}) # 'http://mypage.com/?param=2'
Thx.

To convert a JavaScript object into a URL parameter string you can use the jQuery param method:
$.param({a:1, b:"Test 1"}) // gets: "a=1&b=Test+1"
To parse a URL parameter string into a JavaScript object use this solution.

There is this jquery plugin https://github.com/allmarkedup/jQuery-URL-Parser that I used once. But once you console.log window.location you will see that it is not so hard to do it your self.
I never tried this one: http://urldecoderonline.com/javascript-url-decode-jquery-plugin.htm but it seems it can build URL to.
Have fun

Related

How to relevant data get from PHP api

I need to append params to my query, but I always get different results. I tried a few ways but without success. Check code below.
First check my code which work without new URL (I need to use new URL).
let url = `${getBaseUrl()}/myUrl/search?surnames=${allData.join('%0A')}`
This works well, but when I use new URL:
let url = new URL('myUrl/search' , getBaseUrl())
url.searchParams.append('surnames' , allData);
The code above doesn't work, and I don't know the reason why?
I tried to inspect url and see different
Works: search?surnames=smith%0Ajordan
Doesn't work: search?surnames=smith%250Ajordan
The only difference is inside between "smith" and "jordan"
With %0A work
With %250A doesn't work
Not sure how this was generated.
The original % is being encoded as %25, so %0 becomes %250.
Try using an unencoded line feed (\n):
const
allData = ['smith', 'jordan'],
getBaseUrl = () => 'https://localhost:8080',
url = new URL('myUrl/search' , getBaseUrl());
url.searchParams.append('surnames', allData.join('\n'));
console.log(url); // https://localhost:8080/myUrl/search?surnames=smith%0Ajordan

Postman - Get only endpoint from a URL

Having this kind of endpoing:
https://hey.com/p1/m/p2
I want to get rid of the https://hey.com and get only the /p1/m/p2 part on the Pre-request Script. I know I can do it using request.url.replace(/^.*\/\/[^\/]+/, ''), getting the desired output /p1/m/p2.
Is there any other way to do it without using replace or regex? Something like request.url.pathname (which is not working, obviously).
The URL above is just an example, the endpoints and urls will vary.
Bear in mind that I'm using the Pre-request Script on the Postman environment and some things may not work.
Thank you so much.
You should be able to use the URL browser API to construct a URL object.
(new URL(request.url)).pathname
If you're using the desktop version, you can use the built-in node.js API.
var URL = require('url');
URL.parse(request.url).pathname
You can try something like this
var fullUrl = request.url.split('/');
fullUrl.splice(0,3);
var url = a.join('/');
console.log(url);

Append url part into main url using angularjs or javascript

I have a url path as shown below.
http://localhost:12534/urlpart1/urlpart2?querystring=140
So I need to replace the "urlpart2" with the "urlpart3" by using javascript or anugularjs.How can I do that.I can get the whole url by using:
var urlPath = window.location.href;
Any help would be highly appreciated.
If you just need to replace specific substring urlpart2 you can go with simple replace method:
var urlPath = window.location.href;
var newUrlPath = urlPath.replace('urlpart2', 'urlpart3');

How to detect URL in JS?

How do I detect a URL using javascript?
For example, if i have mywebsite.com/video/r739cw3#1, I want to execute a JS function that will convert this URL to a link
var url = window.location.href;
Try this document.URL
for Example
alert(document.URL);

To get path name with url values in jquery

This is an example URL.
http://stackoverflow.com/questions/ask?a=1&b=2
question: I need to fetch path name along with url values like this/questions/ask?a=1&b=2
I need jquery or javascript solution
var url = document.createElement('a');
url.href = 'http://stackoverflow.com/questions/ask?a=1&b=2';
alert(url.pathname + url.search);
Demo: http://jsfiddle.net/karim79/8XYYj/

Categories