How can I include special characters in query strings? - html

URL http://localhost/mysite/mypage?param=123 works fine. However, if I want to put some special characters in param, like ?, /, \, then the URL becomes http://localhost/mysite/mypage?param=a=?&b=/ or http://localhost/mysite/mypage?param=http://www.example.com/page2?a=\&b=... which won't work. How do I resolve this issue?

You have to encode special characters in URLs. See: http://www.w3schools.com/tags/ref_urlencode.asp

You need to encode the query parameters before combining them to form a url. The function needed here is encodeURIComponent.For example,
the url you need to create is:
http://localhost/mysite/mypage?param=a=?&b=/
Now, assuming that ? and / comes as variables, you need to encode them before putting in the url.
So lets create your url using this function(I am expecting two query parameters):
var q1 = "a=?"; //came from some input or something
var q2 = "/"; //came from somewhere else
var faultyUrl = "http://localhost/mysite/mypage?param="+ q1 +"&b=" + q2;
// "http://localhost/mysite/mypage?param=a=?&b=/"
var properUrl = "http://localhost/mysite/mypage?param="+ encodeURIComponent(q1) +"&b=" + encodeURIComponent(q2);
//"http://localhost/mysite/mypage?param=a%3D%3F&b=%2F"
This function is in basic JS and supported in all the browsers.

Easy way to pass QueryString value with special character using javascript:
var newURL=encodeURIComponent(uri);
window.location="/abc/abc?q="+newURL;

In JavaScript you can use the encodeURI() function.
ASP has the Server.URLEncode() function.
You can use HttpServerUtility.UrlEncode in .NET.

You need to use encode special characters, see this page for a reference.
If you're using PHP, there's a function to do this, called urlencode().

I did below, it works fine.
const myQueryParamValue = "You&Me";
const ajaxUrl = "www.example.com/api?searchText="+encodeURIComponent(myQueryParamValue)

You need to substitute the characters with URL entities.
Some information here.

Related

Display value + text using jQuery

I am a total beginner. I have a form in HTML and am trying to calculate a specific value using jQuery. I want this value to be displayed in paragraph <p id="final"></p> under the submit button, but am actually not sure, why my code isn't working.
jQuery(document).on("ready", function() {
jQuery("final").hide();
jQuery("#form").submit(function(e){
e.preventDefault();
const data = jQuery(this).serializeArray();
/*
some calculations
*/
$('#final').html($('#final').html().replace('','result + " text"'));
jQuery("#final").show();
}
}
Do you have any idea, what could I be doing wrong??
You've got a several issues here.
Firstly, don't mix jQuery and $. If you're using the former, it's normally to avoid jQuery's alias, $, from conflicting with other code that might use $.
Secondly, you don't actually do any calculation (from what I can see in your code), so I'm not sure what you're wanting to output. I'll assume you're going to fill that in later.
Thirdly, jQuery('final').hide() is missing the # denoting you're targeting by element ID.
Fourthly, the line
$('#final').html($('#final').html().replace('','result + " text"'));
...doesn't quite do what you think it does. For one thing, it makes no reference to your data variable. And running replace() on an empty string doesn't make much sense.
All in all I'm guessing you want something like (note also how I cache the #final element - that's better for perforamnce):
jQuery(function() { //<-- another way to write a document-ready handler
let el = jQuery('#final');
el.hide();
jQuery("#form").submit(function(e){
e.preventDefault();
const data = jQuery(this).serializeArray();
let calc = 5+2; //<-- do what you need to here
el.html(calc).show();
}
}
Guessing result is your variable and your above code is your current status, you should fix the html replacement to something like (depending on your acutal usecase):
$('#final').html(result + " text"));

How to construct a HTML link to prefill a form field on this site

i usually have no trouble constructing HTML links to prefill any given form field.
However, this one has me stumped:
https://www.planningportal.nsw.gov.au/spatialviewer/#/find-a-property/address
I want to construct a link to prefill the address here, I've used all the usual structures like the following:
https://www.planningportal.nsw.gov.au/spatialviewer/#/find-a-property/address#mat-input-0=100+Princes+St,+Ryde+NSW+2112
Also replacing the # with a ? does not work
I believe the field ID is "mat-input-0"
Is there something obvious I am missing here?
Is the site sanitising the query from the end of the link for some reason perhaps?
var url = "https://www.planningportal.nsw.gov.au/spatialviewer/#/find-a-property/address#mat-input-0=100+Princes+St,+Ryde+NSW+2112"
var address = url.split('mat-input-0=')[1].replace(/\+/g, ' ');
document.getElementById("address-input").value = address;
<input id='address-input' val='' size='50' />
you can replace url by var url = window.location.pathname; to get the parameters in your url.
hope it helps :)

Prevent colon in URL from changing to %3A

Is it possible to have a colon from a form go into a link to populate a form on a new page? At the moment the link keeps changing to %3A.
For example, current: http://www.test.com/?field=123%3A456
Trying to have: current: http://www.test.com/?field=123:456
You can't.
You need to decode the parameters
for serverside php you can use
http://php.net/manual/en/function.urldecode.php
$field = urldecode($_GET['field'])
for javascript you can use
decodeURIComponent('123%3A456');

URL check in actionscript

I am a novice in actionscript. I am trying to make an if condition for URL check. I want to call a method if a string contains a URL:
Regular expression
var pattern:RegExp = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/;
Pseudo code
if(string.contains(pattern)){
//do something
}
How can make this if condition in actionscript?
You can validate a URL/(any String) in actionscript by using RegExpValidator, there are lots of way to validate data(string) but it is best way to validate all data(string). it follow all the concept of javascript's RegExp
you can learn more about RegExpValidator on http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/validators/RegExpValidator.htm, Check this url
Also its live demo of regular Expression in actionscript
http://www.regexr.com/
http://ryanswanson.com/regexp/#start
http://probertson.com/resources/projects/actionscript-regexp-test/RegExpTestHarness.html
may any of the one will help you!!!
if (string.search(pattern) != -1) {
//do something
}
If there is no match, search returns -1.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#search%28%29

Trim not working in Coldfusion 8.0

I am trying to trim a string returned by one of my Coldfusion component but whatever I do Coldfusion add line feed at the start of the String without any reason resulting in an error in my Javascript. Do you have any idea of what's wrong with this code?
function back(){
window.location = <cfoutput>"#Trim(Session.history.getBackUrl())#"</cfoutput>;
}
The code above produce the following peace of HTML:
function back(){
window.location = "
http://dummy_server_address/index.cfm?TargetUrl=disp_main";
}
Looking at the Coldfusion specs here is the trim definition :
A copy of the string parameter, after removing leading and trailing spaces and control characters.
So it should have done the job! I am therefore wondering how to do that properly, I don't want to use replace or some similar function.
EDIT : very surprisingly this is working... but I don't like this solution, so if you have any other idea, or at least explanations about this behaviour.
<cfset backUrl = Session.history.getBackUrl()>
function back(){
window.location = <cfoutput>"#backUrl#"</cfoutput>;
}
Make sure your History component has output disabled. i.e:
<cfcomponent output=false >
Then make sure the getBackUrl function (and every other function) in the CFC also has output=false set.
Also, don't forget to use JsStringFormat on the variable, to ensure it is appropriately escaped:
<cfoutput>"#JsStringFormat( Session.history.getBackUrl() )#"</cfoutput>
Otherwise, there's a potential risk for JavaScript injection, or just JS errors, if the URL happens to contain ".
I've tested your current code and it works fine for me, I suspect that your CFC might be returning more then you think, which I obviously can't duplicate. I would personally always ensure that the component returns 'clean' results rather then removing junk characters after the fact :)
I have had similar issues in the past and it has always turned out to do with cfoutput, never got to the bottom of it. As a starting point I would rewrite this way and see if it makes a difference...
<cfset variables.stWindowLocation = '"' & Trim(Session.history.getBackUrl()) & '"'>
<cfoutput>
function back() {
window.location = #variables.stWindowLocation#;}
</cfoutput>