Variables are not appended in url in javascript - 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.

Related

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.

How to display <a> tag from JavaScript in HTML by escaping characters

I have a variable in my JavaScript, where I need to pass an anchor tag and a URL to route. But I am getting errors in my JS file and it looks like I need to escape my HTML tags, but it didn't work.
Can some one help?
var link = "<a href="http://localhost:3002/#/login?customerid=" >Continue</a>;
I tried below but it didn't work:
<a href="http://localhost:3002/#/login?customerid=" >Continue</a>
var link = '<a href="http://localhost:3002/#/login?customerid=" >Continue</a>';
The error was because if you open a string with " you have to end it with " but your url inside href is quoted with " " so it obviously failed to make your string variable. I just quoted your string with ' and let the " for the href.
You need to rewrite the line as
var link = "<a href=\"http://localhost:3002/#/login?customerid=\" >Continue</a>";
The reason is because Javascript thinks that the string ends at the first ". To prevent this, the backwards slash specifically delineates this as part of the string
However, what are you using this for in the first place? Storing HTML in strings isn't recommended.

How do I keep + sign as is in encodeURIComponent("+")?

In a part of the code I can't change. The function encodeURIComponent() will be executed on the URL i pass in, how ever one of my API calls contains a + sign which is necessary to send as a + sign. Right now it gets replaces "%2B" which makes the API fail..
I have tried using escape and "%2B" and backslash infront of my + sign as "+" but nothing gives so far..
How do I make encodeURIComponent('+') return + and not "%2B"
Thank you affordtime for your help
Temporarily swap out the plus signs for an arbitrary marker, e.g.
encodeURIComponent("one £ + £ two".replace(/\+/g, '*PLUS*')).replace('*PLUS*', '+');
Gives you:
"one%20%C2%A3%20+%20%C2%A3%20two"
...ie retains the +, which will also survive the reverse trip via decodeURIComponent().
You can't do it without changing the code. encodeURIComponent will never output a + sign.
If you or someone else can change the code you could use this answer:
encodeURIComponent(search).replace(/%20/g, "+");
and then use spaces in the input where you want + to be.
It is not usually recommended to overwrite native functions but you could do this which would redefined encodeURIComponent to not escape plus characters but otherwise escape the same set of characters.
function encodeURIComponent(s) {
// encodeURI leaves these chars untouched ##$&=:/,;?+
return encodeURI(s).replace(/[##$&=:\/,;?]/g, function(c) {
return '%'+c.charCodeAt(0).toString(16)
})
}
As you can't change the behavior of encodeURIComponent, the simplest way is to replace %2B-s back to +-es:
encodeURIComponent('1+2=3').replace(/%2B/g, '+') //1+2%3D3
This is more efficient, as it needs a single replacement, and doesn't need intermediate "escaping", and simpler, as you don't have to reimplement encodeURIComponent and using the native one might be even faster for large strings.

how to pass special character '/' through a url

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

Insert one string into another

I need to insert 'embed' into the middle of a YouTube URL, so e.g.
https://www.youtube.com/watch?v=JDCV_cK1L1A
Needs to be modified to:
https://www.youtube.com/embed/watch?v=JDCV_cK1L1A
Using only jQuery.
Do I need to use RegEx for this? It seems the only answers I can find online are either appending / prepending, or inserting a string into x position of another string... but neither solution are appropriate for what I need to achieve
You don't need a regular expression for this. If you want to add the folder right after the domain, you can use a plain replace:
url = url.replace('youtube.com/', 'youtube.com/embed/');
If you want to add it as the last folder, you can find the last slash and insert it there:
var last = url.lastIndexOf('/');
url = url.substr(0, last) + '/embed' + url.substr(last);

Categories