yesterday, a friend asked me if it was possible to hide the GET query string (the string followed by the '?' question mark =P) from the URL, so users visiting the website wouldn't be able to see or catch the sent variables. I came out with various ideas:
The firs and most recommended: use POST instead of GET method xD. while GET shows all query string in the URL and can only send a limited 1kb string, POST method have no limitations and sent variables are hidden.
However, if still you're enough stubborn, I figured out two methods using JS that allows you to hide the GET string:
1.- using AJAX (the hard but preferred method)
use AJAX to send a request, this allows you to send both, GET and POST and get the response
without user can see anything. (I'll submit the code for this latter)
2.- using the BOM (the easy and fast method)
the BOM (Browser Object Modeling) has an object called 'location' which contains the full string of the actual browser location (example: 'http://www.example.com'). location has an attribute called search, the search returns the location starting from the question mark '?' which in our case will be the GET query string.
search can return or set data:
if used as: var query = document.location.search;
will return the search stirng.
if used as: document.location.search="something";
will set the search string to "something".
in this example I used both.
//Clean Search
function cleanSearch(){
if(document.location.search.length>1){
document.location.search="";
}
}
first we check if variables has been sent with GET method:
document.location.search.length>1
if so, then we just clean the search:
document.location.search="";
that easy.
note that:
1.-JS most be enabled in the navigator and the GET query will be shown while the js loads (in any case this will takes a reaaaaaaaaaally few moment to load since are just 6 lines of code).
2.- since you're using document.location.search="", this will cause a web refresh. so the only way to use this code is: geting GET string, using it and then obligatory beeing refreshed. (so if you got the point, you'll realize this has lots of limitances)
any other suggestions are welcome.
hope this'll help.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment