How to pass HTML code in query string - javascript

I need to pass html code with QueryString because im using ajax method to send post(i mean comment not the type of method) on my web site. When I write a post like that.
"Hi everybody<br />Whats'up."
its just taking "Hi everybody" removing rest of the content.
Info : Im using GET Method

Don't use escape, it's a deprecated function. Use encodeURIComponent instead:
encodeURIComponent("Hi everybody<br />Whats'up.");
Also, don't forget about Internet Explorer's 2,083 character limit in the address bar, you should use POST instead of GET if you want to avoid it.

What method are you using - GET or POST? You should be using POST. That will allow you to send full html and you don't need to use the querystring.

http://www.mywebsite.com/index.html?html_code=Hi%20everybodyWhats'up.

Related

How to decode part of an Ajax call QueryString in c#

I am encoding the query string in an Ajax call in Javascript using encodeURL() but I can't find a way to then decode it on the serverin c#.
I use HttpContext.Current.Request["ID"] to get the parameter values from the query string and in the debugger I can see that Request.QueryString is fully encoded but when I try to get the value of a particular parameter it wont Decode.
I have tried using HttpUtility.UrlDecode(HttpContext.Current.Request["ID"]) but it doesn't appear to decode it. What am I missing?
Or should I be using a completely different way too get the query values?
Ah sorry!!! - sometimes you just need someone else to ask you a question and then you see the answer yourself - on the client I was using encodeURI instead of encodeURIComponent.

IE : window.open url which is more than 2,083 characters

I am getting a url from server with lot of data as query string
(E.g. http://www.test.com/?n=1,2,3,4,5,6,7,8.....100000) and I want to open it using window.open().
But the moment I pass the URL to window.open the url which gets gets truncated. After searching for sometime I could figure out that the maximum limit for URL is 2,083 characters(IE) so it passes PART of query string and truncates the rest..
How can I overcome this?
Please let me know if I need to provide more details.
I think the only solution is to use POST instead of GET. Just use a form, instead of window.open. Please see this older answer:
https://stackoverflow.com/a/17089124/907420
You could try URL shorteners, like goo.gl or bit.ly:
https://goo.gl/
Speaking of programming, you could try to shorten your URL-s yourself, for the given example:
Exact URL for your example, shorter by ~2000 characters.
Where x..y is translated on server side as range(x, y) -> 1,2,3,4,5...100000 for x and y being 1 and 100000
If you want to stick with GET (and I would recommend that you use POST) you can try to compress the parameters. Instead of giving a huge number of parameters you create a javascript object holding the parameters, jsonfy it and you end up with a string that can be compressed and uncompressed again at the other end. Afterwards you can deserialize the JSON string and you have your parameters. Depending on the number of parameters it might still not be sufficient for a GET request.
But at the end a POST request is the best solution I think.

Encoding URL GET variable

I want to ask you how can I safely encode multiple GET variables from my URL and put them in one, then it send it to another page.
Thank you for your time!
If your intention is to increase security of the data being sent through URL then you should go for POST method instead of GET. Otherwise if you are bound to use GET then use some encryption(A way of encoding) standards e.g. MD5 on the data before you put them in the URL using GET method.
There are functionalities available on server side that can be used for the encryption.

Read Rewritten URL - javascript

I need to read the original URL from the rewritten path.
this is how i do url rewrite in my webapplication. I don't have codebehind in my application. So i read everything through javascript and call a webservice. While calling the webservice, i need to read query parameter ID and pass it.
Is my url rewrite method is right? if so, how can i read the original URL i.e. pages/products.aspx?ID=123. Because in my browser address bar it shows only product.aspx, i get the same through javascript.
Don't know whether right or wrong way, anyhow I managed to get the query string using JavaScript.
The query string was not visible in the browser window, but in the form tag it is available. So I get that using document.getElementsByTagName('form')[0].action, and completed my task.

Encoding user input as URL GET parameter in JavaScript

I'm writing a JavaScript function that needs to uphold three properties:
be very small and lightweight - no external libraries
encode a string in such a way as to be able to be passed as a GET parameter
this string must be decoded again at its destination
Effectively, it authenticates the user by sending his username and password to a PHP page which then verifies it. This is done via GET because I haven't yet found a way of doing a background cross-domain POST request. The trouble is that if the user has a character such as '#' or similar in his password, it doesn't get sent properly.
Currently to avoid this, I encode() the password string before sending it, which allows it to be received without problems. However, I read that PHP's urldecode() is not a perfect analog for this, as there are corner cases which are treated differently (i.e. ' ', '+', etc). Sadly I cannot find this document anymore, so I cannot quote it, but the gist was that one of them converts spaces into '+' signs, which the other treats as an actual plus sign, or something like that...
As such, I'm looking for a Javascript function that can take a string and make it URL-safe, and which has a perfect reversal function in PHP so that the original string can be recovered.
The arguably awful code I currently use to achieve this:
login.onsubmit = function(){
loginFailMsg.style.display = 'none';
var inputs = login.getElementsByTagName('input');
var formdata =
'username='+inputs[0].value+'&password='+encode(inputs[1].value);
submit.src = formtarget+'/auth/bklt?'+formdata;
userinfo = undefined;
setTimeout(getUserinfo,300);
return false;
};
encodeURIComponent, PHP will decode it automatically when populating $_POST or $_GET
'&password='+encode(inputs[1].value)
Where's encode function coming from? Seems to me the quick answer to your question is using encodeURIComponent() instead, available since JavaScript 1.5. See also Comparing escape(), encodeURI(), and encodeURIComponent(); it does not encode everything either, but does encode all the server expects it to.
(As for cross-domain AJAX POST calls, I'd really have a look at "JSON with Padding". See JSONP with jQuery that I mentioned in the comments earlier. This will also prevent issues with the timeout you've randomly chosen, and jQuery will also help you, a lot, to get rid of inputs[0].value and the like. And, as you apparently already have a MD5 hash on the server, I'd really hash the password client side as well --see Karl's answer-- and compare those hashes instead. Respect your user's password and your own time, drop that no external libraries requirement!)
I don't think there's such a thing as a reversible hash function. There are plenty of javascript md5 libraries available, however.

Categories

Resources