I've got a Javascript script that sends 4 variables to a php page.
The fact is that, if one (or more) of these variables contain a character such as "&" or "%", it just
won't work, it only sends what there was before "&" or "%". For instance, if the content of a variable is
"Bolliger&Mabillard", the result will be "Bolliger". Obviously, this happens because the script separate each
variable with an "&", how can I solve this?
Here's the script I'm talking about
<script type="text/javascript">
function save()
{
var shortname=location.hostname.replace(/^(www.|)([\w]+)(\..+|)/g, "$2");
var url="http://"+window.location.hostname+"/";
var link=window.location.href;
var link_title="Bolliger&Mabillard";
window.open("http://www.example.com/add.php?url=" + url + "&shortname="+shortname + "&link="+link + "&link_title="+link_title);
}
</script>
You need to URL-encode the values using encodeURIComponent().
Related
I have a variable written from PHP into my javascript (using json_encode), that looks a little like this:
mappoints[x]['about'] = 'Administrator: Foo Barlt;br />Telephone: 555-4202<br />Email: bert#hotmail.com<br />Website: www.domain.com'
That I am using for a google maps map point. Using the json_encode seems to be very picky about what characters I can and cannot enter into it, so i am wondering how I can convert those special html characters into real html characters using javascript?
update
The way i am building my variable is:
var description = "<h3 style='margin: 0; padding: 0;'>" + mappoints[x]['name'] + "</h3><b>Director:</b> " + mappoints[x]['director'] + "<br/>" + mappoints[x]['about'];
The HTML in the varaible is all fine, until I add the about index. No function I have attached or tried yet seems to give me proper HTML.
You can use the dom to decode those entities for you.
mappoints[x]['about'] = 'Administrator: Foo Barlt;br />Telephone: 555-4202<br />Email: bert#hotmail.com<br />Website: www.domain.com'
mappoints[x]['about'] = $('<div/>').append(mappoints[x]['about']).text();
http://jsfiddle.net/5FTCX/
Basically when you add the html to the dom it will show the entities as the characters they represent, using .text() you can receive the data back as you'd see it in the browser as text, not html with the entities. If you want back the html you can use .html() e.g..
Would it be okay with :
return mystring.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
From here : Convert special characters to HTML in Javascript
Just because #Musa's idea was great but needed some re-interpreting on my side, I wish to post a quick function here, that will handle htmlspecialchars great, based on #Musa's design :
function htmlspecialchars_decode(string) {
$('body').append("<span style='display:none;visibility:hidden;' id='tempText'>" + string + "</span>");
var result = $('#tempText').text();
$('#tempText').remove();
return result;
};
try this
decodeURIComponent(str) or `unescape(str)` or `decodeURI(str)`
I have ajax call in my jQuery code with which I am doing lookup with C# web method.
The call works normally most of the time, the user needs to enter last name and it will get all the results for that last name.
The problem happens when user will enter last name with single quote included such as O'leary.
This is the jQuery line I am using:
data: "{selectedAgent: '" + $('#<%=txtAgentNameText.ClientID %>').val() + "',
companyID: '" + $('#<%=ddlCompany.ClientID %>').val() + "'}",
The problem happens with the txtAgentNameText.ClientID value.
It looks like you are passing a JSON string to your jQuery ajax method. Why not just pass a javascript object?
var data = {
selectedAgent:$('#<%=txtAgentNameText.ClientID %>').val(),
companyID: $('#<%=ddlCompany.ClientID %>').val()
};
$.ajax({...data:data...})
Okay so the escape character is \ so you could replace \' or you could change your format so the last name is contained in double quotes! Both ways should suffice
I'm trying to insert a variable's value into a url, but it's not working; I'm just getting the variable not the value
'myid' and 'verif' are the variables and their values are integers.
This code inserts the url into a hidden field in a form
$('#return').val(http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=myid&packid=1&verif=verif&jcode=xxx111xxx);
How do I write the following url so the variables 'myid' and 'verif' are converted to their values?
Well you are missing quotes so your code would not work at all.
$('#return').val("http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=" + myid + "&packid=1&verif=" + verif + "&jcode=xxx111xxx");
You should probably use encodeURIComponent()
You need to quotes " " the strings and concat the variables +
Try
$('#return').val("http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid="+myid+"&packid=1&verif="+verif+"&jcode=xxx111xxx");
JavaScript does not support string interpolation. Try something like this.
myIdVal = encodeURIComponent(myId);
verifVal = encodeURIComponent(verif);
var url = "http://pegdown.com/index.php?option=com_joom_creditspack&controller=paypal&view=paypal&layout=return&task=checkout&myid=" + myidVal + "&packid=1&verif=" + verifVal + "&jcode=xxx111xxx";
$('#return').val(url);
A simple string works for me:
given index = 2,
`a.setAttribute("href", "myDirectory/" + index + ".jpg");` links the anchor to
"myDirectory/2.jpg", ie. the file number is taken from variable index.
Not sure if the setAttribute tolerates multiple tokens in its second parameter, but generally, this works.
OK, I'm a javascript/grails novice and I'm not sure what to do here.
Basically I have a javascript function that is being called with multiple parameters and I want to substitute them into a grails parsable string.
I have a number of grails drop downs that call a javascript function to link to another page with multiple parameters that need to be passed (item number and quantity).
Here is the select:
<g:select optionKey="key" optionValue="value" value="${item.getQty()}" name="qty" from="[1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9]"
onchange="goToPage('qty${item.id}',this.value)"></g:select>
The javascript function:
<script type="text/javascript">
function goToPage(itemId, val){
window.location.href="${createLink(controller:'GOrder' ,action:'updateShoppingCart' params:[item: "", qty: ""])}" + itemId + val;
}
So, it takes the itemId and val, concats them and replaces the last set of quotes with that concatenated value. What I want to happen is for each of the parameters to replace one of the sets of quotes.
I really don't understand how what looks like concatenating a string actually substitutes a value.
Thanks!
You're getting your server and client sides mixed up here. The
${createLink(controller:'GOrder' ,action:'updateShoppingCart' params:[item: "", qty: ""])}
is evaluated by Grails on the server side when rendering the GSP into HTML, so what you end up with in your JavaScript will be something like
function goToPage(itemId, val){
window.location.href="/myapp/GOrder/updateShoppingCart?item=&qty=" + itemId + val;
}
What you probably want instead is to generate just the base URL up to the question mark on the Grails side, then add the query parameters on the client side in JavaScript
function goToPage(itemId, val){
window.location.href=
"${createLink(controller:'GOrder', action:'updateShoppingCart'
).encodeAsJavaScript()}?item=" + encodeURIComponent(itemId)
+ "&qty=" + encodeURIComponent(val);
}
Note the way I've added encodeAsJavaScript() - you should always use this when generating values that are to be included in a JavaScript string literal, it will backslash-escape anything that needs it. Similarly any JavaScript string you're adding to a URL needs to be encoded using the JavaScript encodeURIComponent function - if the value of itemId were "item one", for example, you need to append item%20one to the URL.
in a JSF facelet page (.xhtml) I have this javascript code
<script type="text/javascript">
function navigateToDetail() {
var id = document.getElementById("idElemento").value;
alert(id);
var isPratica = document.getElementById("isPratica").value;
alert(isPratica);
var box = "#{boxCtrl.idBox}";
alert(box);
if (isPratica==true)
window.location = "DettaglioRichiesta.xhtml?id=" + id + "&box=" + box;
else
window.location = "../Richieste/DettaglioRichiesta.xhtml?id=" + id + "&box=" + box;
}
</script>
It doesn't work because the jfs engine think that "&box" is relative to a bindign, and it says:
Error Parsing /Box/ListaRichieste.xhtml: Error Traced[line: 20] The reference to entity "box" must end with the ';' delimiter
I can I avoid this behaviour?
Facelets is a XML based view technology. The & is a XML special character. It's interpreted as start of a XML entity like , , etc. It is therefore looking for the end character ;, but it found none, so it is throwing this error.
To represent the & literally inside a XML document, you need to use & instead of &.
window.location = "DettaglioRichiesta.xhtml?id=" + id + "&box=" + box;
You can also just put that JS code in its own .js file which you include by <script src> so that you don't need to fiddle with XML special characters in the JS code.
<script type="text/javascript" src="your.js"></script>