how to pass special character '/' through a url - javascript

My url contains a special character '/'.Example :- localhost:8080/name=asdasd 11/2/2015. When I pass this through encodeURI to the spring framework , I need to differentiate '/' which is present in my variable and the one which is the actual separator. I have tried using './*' which returns me the string after the seperator but in case I have two args in my url which contain '/' then the above solution fails. How do I solve this ??

Are you looking for like this. Encode it only values.
var myUrl ='localhost:8080/name=' + encodeURIComponent('asdasd 11/2/2015');

Related

Regex to replace number in url

Similar to many questions such as Javascript Regex url replace
But I'm trying to replace a number in a URL string i.e.
filename.replace('org\/*\/','org/23/')
The URL is much longer, but I just need to replace the number that comes after org/
ie. assets/org/1/course/154/805597a6-9c35-4f13-af83-ebfdcb12f769/upload_87bf778b-44ee-4a39-8765-ee9c4b9f3126.jpg
The current regex you're passing is being interpreted as a string. You need to use the forward-slashes or RegExp class to indicate you're passing a regex
let filename = "assets/org/1/course/154/805597a6-9c35-4f13-af83-ebfdcb12f769/upload_87bf778b-44ee-4a39-8765-ee9c4b9f3126.jpg"
console.log(filename.replace(/org\/([0-9]+)\//,'org/23/'))

Sending URL request resulting in invalid JSON

I have this string with two variables inserted inside it
URL='https://bla.com/api/multicomplete?data={"query":"' + title_text + " " + city_name + '"}';
Sometime, title_text includes some wacky characters (&, $, letters like đ etc..) and it results in something like this:
title_text = 'Airport Hotel Park & Fly Sofia'
...?data={"query":"Airport%20Hotel%20Park%20&%20Fly%20Sofija",...
I can assume that that is because I have %20&%20 in URL, and indeed when I remove &%20 (& space), then I get no errors.
So I have tried this method:
JSON.stringfy(title_text)
to let me send those characters via URL, but it doesn't work. Is there a good method to escape those special characters in that long string? I don't feel like removing them for good, I feel so dirty thinking of it.
You have to use URI Encoding using encodeURI() to solve this problem.
You can do JSON.stringify and concatenate with the base url just like you've already done.

Forward slash in window.location.href redirects to root url

I'm generating window.location.href attributes where the path can sometimes include a slash "/".
ImageButton.Attributes.Add("OnClick", "window.location.href='DynamicItemDetail.aspx?Partno=" & strItemCode & "&Decorloc='")
The final strings look like this:
window.location.href="myurl.com/products.aspx?_Category=130&Partno=WWS-AWT/SWD&Decorloc="
Unfortunately, since the item code contains a slash, window.location redirects to the root url. Is there anyway to tell Javascript not to treat the slash as a subdirectory?
You will need to escape the characters in the URL.
encodeURIComponent is what you are looking for.
var encodedItemCode = encodeURIComponent(strItemCode);
ImageButton.Attributes.Add("OnClick", "window.location.href='DynamicItemDetail.aspx?Partno=" + encodedItemCode + "&Decorloc='")
The resulting URL will be
myurl.com/products.aspx?_Category=130&Partno=WWS-AWT%2FSWD&Decorloc=
Use the encodeURIComponent() Javascript function to encode the slash and any other special characters into a format that's allowed to be used in a URI.
A good reference for the function is here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
You need to escape the slash as %2F
Yes. You should URL encode the string, or the slash. So, instead of "/" you use "%2F"

How to get email (having '+') from URL in Parameters in Ruby

When I hit the page /login?email=abc+dev#xyz.com
In Ruby's Controller,
The Parameters are: {"email"=>"abc dev#xyz.com"}
+ is missing in this parmas.
What is the correct way to get email in parameters ?
encode URI will give me same(that is, URI::encode("abc+dev#xyz.com") is equals to "abc+dev#xyz.com")
You can use Rack::Utils.escape()
Rack::Utils.escape("abc+dev#xyz.com")
# => "abc%2Bdev%40xyz.com"
Or with Javascript you can use encodeURIComponent()
encodeURIComponent("abc+dev#xyz.com")
"abc%2Bdev%40xyz.com"

Variables are not appended in url in javascript

what i Need
i need that on appending variable data url should in proper format.
url
url : "http://local-login.times.com/ticket_transaction/autosubmit_advance/"+evt_id/user+"?visitor_flag=1&source=get-direction-oneclick.
value of evt_id=2244 and user=45566.
problem is on appending these variable in url in jquery.
on click on link
output
http://local-login.10times.com/ticket_transaction/autosubmit_advance/0.012915232373744963?visitor_flag=1&source=get-direction-oneclick
but output should be
http://local-login.10times.com/ticket_transaction/autosubmit_advance/2244/4566?visitor_flag=1&source=get-direction-oneclick.
where i have done wrong.
any suggestion are most welcome.
You are dividing the numbers in the string
+evt_id/user+
^
Hence why you are getting a decimal output.
+evt_id + "/" + user+
^^^^^^^^^^
If you do evt_id/user, it will divide the numbers.
Do evt_id + "/" + user instead if you want to do concatenation.
See how to use the + operator for concatenation on this MDN page.

Categories