I am trying to do a call to url
http://example.com/controller/action?var1=CD14+%20MDM%20in%20GM-CSF%203%20days%20then%203%20days%20IFN-%CE%B3&var2=56&var3=ENSG00000115415
If you look at var1 that I am passing value 'CD14+ MDM in GM-CSF 3 days then 3days IFN-γ'.
When in the controller I try to get this var 1 using request.params.get("var1"), i get 'CD14 MDM in GM-CSF 3 days then 3days IFN-γ' with missing + and instead getting extra space after CD14
How can I pass + in my variable in url
This has an answer here. It says that + needs to be encoded as %2B.
If you are intended to use plus as a data component, instead of url component, You should pass %2B instead.
Javascript :
var url = "http://example.com/controller/action?var1=CD14+%20MDM%20in%20GM-CSF%203%20days%20then%203%20days%20IFN-%CE%B3&var2=56&var3=ENSG00000115415";
//NOTE: this following wil lreplace all + to %2B
url = url.replace(/\+/g,"%2B");
console.log("new : ",url);
Try '%2B' (as other have stated).
But if you are in Javascript, you could just use the built-in method for doing URL encoding called "encodeURIComponent". Note you need that and not "encodeURI" which will not encode reserved characters like '+'. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
Related
I want to generate a qr code with multiple pre-filled values. But only the first value seems to be encoded into the generated qr code. I guess because it's assuming parameters after the first belong to the api url..
Can anyone help me here?
.src = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data="
+ "https://google.com/?" + "field1=" + var1 + "&" + "field2=" + var2
I've looked online without luck - perhaps because I don't know how to describe the problem. (note that google.com is jut a placeholder url, and not the one i'm using)
Since the URL you are trying to pass to data contains /, ? and & you have to encode it. For instance, the browser wouldn't know whether to pass field2 to the main URL or to the data URL. To encode the URL being passed to data:
const url = "https://google.com/?" + "field1=" + var1 + "&" + "field2=" + var2
const src = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=" + encodeURIComponent(url)
On an unrelated note, consider using template string to make your code more readable:
const url = `https://google.com/?field1=${var1}&field2=${var2}`
const src = `https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(url)}`
I Tried this code to get multiple value in href but it does not work. any problem on this one ?
Print
You are missing a + sign between a string and a value.
The error is between this two
document.getElementById('CUS_CODE_MX').value '&AGE='
Correct format
document.getElementById('CUS_CODE_MX').value + '&AGE='
Every time you join a value and a string, you need a + sign
Even if you are joining two strings
'Hello'+ 'World'
Pliss avoid long js as an inline atribute. I will recommend you call a function as the onclick attribute.
Hope this helps :)
Print
It's better to use external script for that rather than inline format. And just add missing + to your code. Also, using variables would clean up the code.
function func() {
var CUS_CODE_MX = document.getElementById('CUS_CODE_MX').value;
var AGEID = document.getElementById('AGEID').value;
this.href = 'printsales.php?CUSTOMERID='+CUS_CODE_MX+'&AGE='+AGEID;
}
Print
I am writing js function which takes the actual location.pathname + location.seach, so that the user can come back to search result page if he hits back button.
example url: http://127.0.0.1:8000/search_for_book/?titel=&autor=doniyor#
function select_book(bookid){
var backurl = String(window.location.pathname+window.location.search);
//alert(backurl); //<---- this is giving the correct full path
window.location = 'selected/?book_id=' + bookid + '&back=' + backurl;
}
but in the last line, function is appending only till ?titel= and cuts off the rest. the new url is becoming this:
http://127.0.0.1:8000/search_for_book/selected/?book_id=10&back=/search_for_book/?titel=
why is this? i need full location.pathname with full location.search.
any ideas?
but in the last line, function is appending only till ?titel= and cuts off the rest.
No, it doesn’t. Alerting/logging the string value that you’re assigning to window.location would have shown you that. (And btw., window.location.href is the correct way to update the location. location itself is an object, not a property – only the browsers’ error tolerance lets you do it this way. So use window.location.href = … instead.)
It’s going wrong, because & in a URL separates parameters from each other. So the value of your parameter back ends after the &, and then comes a new parameter autor – because you neglected to URL-encode the parameter value properly.
Use encodeURIComponent on the value, before adding it into the string.
So let's say there's a URL http://example.com/index.html/hello/hi where "hello" and "hi" are parameters.
How would you use javascript and forms method POST to extract the parameters?
Your subject is a little bit vague. However, I thought I'd made an example of possibilities.
http://jsfiddle.net/tive/LjbPq/
The idea is to split the URL for each character /, in whatever way you received it.
var parts = document.URL.split("/");
Since split() returns an array (zero based), you need to distract 1 from the total length to get the last index.
var lastPart = parts[parts.length - 1];
Run this in a for loop, and you should get the idea as occurring in the example.
documentation on document.URL to retreive the complete URL
documentation on window.location to use properties of a url (protocol, href, pathname, ...)
this could work...
var secondvar = window.location.href.split('/')[window.location.href.split('/').length];
var firstvar = window.location.href.split('/')[window.location.href.split('/').length-1];
I am able to get the querystring from url like this using regex and javascript: But I need to get rid of these %22...these don't show up in IE, just in FF..How do I do that? Ineeed everything after k=..but without %22..
<script type="text/javascript">document.write('<div class="DynamicSearchTitle">
Showing All Results For ' +
location.href.match(/\&k\=(.+)/)[1]+ ' Matches </div>');
</script>
URL
http://mysite/sites/dev/contact-us/Pages/LocationSearchTestPage.aspx?s=bcs_locations&k=%22Hospital%22%20OR%20%22Office%22
The URL is broken so I can't take a look at the whole code, but I think what you're looking for is the decodeURI-function.
decodeURI("%22")
for example would return "
Unescapeing the url from your question:
decodeURI("&k=%22Hospital%22%20OR%20%22Office%22");
returns &k="Hospital" OR "Office"
You can get all the Query String component by simple JS function described here
Use it like this,
var uparts = getUrlParts(location.href);
var the_K = uparts["k"];