To get path name with url values in jquery - javascript

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/

Related

Decoding url not working properly JS I tried decodeURIComponent

Hi I have a case a URL simillar to this:
https://linkhere?response-content-disposition=inline; filename="placeholder.jpg"; filename*=UTF-8''placeholder.jpg response-content-type=image/jpeg&X-Amz-Algorithm=....sometextcontinueshere
I am trying to decode it like this and I need to take the filename
const decodedUrl = decodeURIComponent("linkhere")
const urlParams = new URLSearchParams(decodedUrl);
const filename = urlParams.get('filename');
console.log(decodedUrl)
But for some reason does not work the decoding properly, do you guys have any idea?
There is nothing built in that is magically going to get the filename since it is not a querystring paramter. The easiest thing you can do it change whatever is building this to have valid querystring parameters so you can parse it.
With what you have, you will need to read the one parameter that has the file name in it. After that you are going to have to parse out the filename from that string.
Basic idea:
var str = `https://linkhere?response-content-disposition=inline; filename="placeholder.jpg"; filename*=UTF-8''placeholder.jpg response-content-type=image/jpeg&X-Amz-Algorithm=....sometextcontinueshere`;
const url = new URL(str);
const parts = url.searchParams.get('response-content-disposition').split(/;\s/);
const fileNameParam = parts.find(x => x.startsWith('filename=')).match(/"([^"]+)"/)[1];
console.log(fileNameParam);

How to fetch the website name from localstorage

I would like to fetch the website name from the localstorage. Generally we use localstorage.getItem(key) to get the value inside the localstorage. But I want the link name itself.
For Eg- from the image I want https://stackoverflow.com as my output.
Local Storage Image Click here
You can just get the name from the url using window.location. You can do this instead
var protocol = window.location.protocol
var hostname = window.location.hostname
var url = protocol+"//"+hostname
console.log(url)

how to manipulate a url with javascript

having some trouble altering a url when the filename will change every time.
this is the url i am returning from api. the pdf name will be different evertime
C:/Users/rsanchez/Source/completePdfs/PdfSharpResult-6-23-2015 2-13 PM.pdf
I need to change it to this
ViewerJS/#../completePdfs/PdfSharpResult-6-23-2015 2-13 PM.pdf
what I am trying so far
var fileURL = result.FileName;
fileURL.replace('C:/Users/rsanchez/Source/completePdfs/', 'ViewerJS/#../completePdfs/')
$scope.returnedPdf = fileURL;
console.log(fileURL)
You are doing it alright. Just assign the update value back to fileURL
fileURL = fileURL.replace('C:/Users/rsanchez/Source/completePdfs/', 'ViewerJS/#../completePdfs/')

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');

jquery url builder/parser

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

Categories