Pass GET variables without the URL? - javascript

Suppose I have these two links on my homepage
http://example.com/user?id=1
http://example.com/user?id=1&sex=male
These two links point to the same page. The users page loads information based on the id.
If the second link is clicked the page loads extra information based on sex.
These two pages are almost identical, therefore not good for SEO.
My question is it possible to pass the second argument without using the url?
Maybe using POST or javascript?
Thanks

Using POST is the answear. Even though I have no idea, how this influences any SEO strategies...

The only other way to pass it without GET would be POST or by using cookies. They may be other methods that I am missing, and please correct my if I am wrong.

Related

Handling search parameters URL PHP

I am wondering if I would be able to get some advice.
I am trying to build a search for my site and I want to link to be reusable so it can be sent as a link to someone else to see the same results.
The problem I have is that there is the possibility to have 100s of parameters for this search, so I dont think a GET requests in the URL are the right way to go.
I was thinking of the possibility of creating a JSON file that saves the parameters in the search and to give it a specific name say "qwer-eweq-qwe" then the URL link could be www.mysite.com/qwer-eweq-qwe
Then once a user would navigate to the URL it would then read the JSON file and pull the correct parameters.
Is there any other ways I could do this? Can seem to find too much online. any pointers would be very helpful
Thanks,
Richard
With so many parameters, please, consider switching way of dealing with it.
An idea would be to use a filter system. One POST method would create a filter with a set of parameters in body (either raw or JSON, does not matter).
POST /filter
The call would create (or not, if already exists) a filter.
The call would return a filter ID that you can use to search by fixed parameters:
GET /search/:filterid
That way, you even can manage cache limitations depending on filters, by invalidating those if needed.

jQuery load or better ajax way?

I am creating a product overview page via PHP and want to add detail information via AJAX. I am using the jQuery .load-function. The thing is I want to add detail information at three different places, each having a different format, while the detail data being about the same.
I wanted to be smart and created one PHP page where the database connection and query for the details are established and the three different kinds of information formats are created and then referenced each information format using the load function's ability to address page fragments, i.e. something like
$("#123").load(bestprice.php?id=123 #part1")
$("#123").load(bestprice.php?id=123 #part2")
$("#123").load(bestprice.php?id=123 #part3")
It works, but of course my hope that some sort of cache would arrange for the page to be loaded only once was disappointed. It loads three times for each id.
So, my questions are:
a) Is there a better way for reaching my goal, i.e. creating an array in the loaded page containing the data that I can use in my original page with doing the formatting there? How would I hand it over?
b) Would it be possible to load the page only once and address the parts with some kind of processing in my original page?
Please, don't answer only yes. Consider me a newbie who needs as much explanation as possible. Code samples are greatly appreciated!

Find the last visited URL in javascript with history

I know about document.referer and this is not what I'm looking for.
It could probably be a solution but I want to retrieve it in local without using server.
Steps :
I'm pushing a new state in the current url of my page with history.pushState('','','#test')
I'm going to the next page.
And now I want to retrieve the URL but history.previous has been erased since Gecko 26.
Is there an other way of getting this value except from using cookies or sessionStorage?
One way of doing it would be to put the value in a parameter of the next page address, like so:
http://server.com/next-page-address/whatever.html?prevData=RequiredStateData
Note that using this approach, you might be exposed to users changing RequiredStateData in a malicious way. You can protect yourself using encryption or some checksum field.
So my problem was that there is no option for that purpose in local without a server environment.
If you find this question it's that you're probably in the same problem as me so the only option I found was to use.
sessionStorage.setItem('foo',val) and retrieve it with sessionStorage.getItem('foo').

What should be the new URL content when using Javascript History API?

I am using AJAX to load content dynamically, in my page. (basically I have a search bar, and the search results are obtained using AJAX).
I want to make use of History API, since, After several searches(and also applying sorting and filter), if the user press browser back button, it takes them directly out of my site, instead of taking them to the previous search result.
There are several online resources about using history API, but I could find any information regarding the URL, that needs to be set using
window.history.pushState(dataObj, title, URL);
If we just set any URL maybe like www.mypage.com/value1, then, this would become a fake URL, which if bookmarked and used later, will not work.
So I was thinking of using the following appoach.
1)Set the URL to www.mypage.com?variable1=value1&variable2=value2 .....
Where value1 and value2 ... would be some information, which I use for AJAX.(In my case these values would be the search text and the filter and sort information)
2)On press of back button, retrieve the URL, spit it using "?" and "&" separators, and take action accordingly
3)If this URL is bookmarked also, I can handle it appropriately, since all the information is available as variables.
So, My question is, Is this a right approach, or is there any better(standard) way of handling such scenario ?
This is somewhat opinionated, but I think your approach sounds fine. So long as the user is able to press the back button and directly visit the URL to get the same results, then I think it is a perfectly fine way of doing it, and I cannot necessarily think of a better way.
In fact, from what I could grasp after looking into the Google search javascript code, it seems like they use a very similar method. They use history.pushState(...) to add search query URLs to browser history, and when they initialize the window, they check the history object as well as the URL to decide what search results to load using JS. That is not to say that Google is the go-to guru for proper web development, but if they are using a method like that, I think it is pretty safe to say that you would be fine to use it too.
I would definitely be interested if anyone has a better way, because that seems to be the best.

client-side data storage and retrieval with html and javascript

I'm building what I am hoping to be a fairly simple, quick and dirty demo app.
So far, I've managed to build a bunch of components using only html and javascript. I know that eventually I'll hook-up a db, but at this point I'm just trying to show off some functionality.
In the page, a user can select a bunch of other users (like friends). Then they go to a separate html page and there is some sorting info based on the selected users.
So my first attempt was to put the selected users object into a cookie, and retrieve the cookie on the second page. Unfortunately, if the user changed their selection, the cookie wasn't getting updated, and my searches on StackOverflow seemed to say that deleting and updating cookies is unreliable.
I tried
function updateCookie(updatedUserList){
jQuery.cookie('userList',null);
jQuery.cookie('userList',updatedUserList);
}
but though it set the cookie to null, it wouldn't update it on the second value.
So I decided to put the selected users object into a form. Unfortunately, it looks like I can't retrieve the contents from the form on the client-side, only on the server-side.
Is there another way to do this? I've worked in PHP and Rails, but I'm trying to do this quickly and simply before building it out into something larger and am trying to avoid any server-side processing for now, which I have managed to do up to this point.
Since this is a demo, can you use HTML5? If so, you can use local storage: link text.
Another option is to use AJAX to load the other HTML page (replace whole body of the current document). Your storage variables would be stored in the <head>. This is a tightly coupled design, but again you're making a quick and dirty demo.
Is updatedUserList a string? If it's an array you might have to stringify it first:
jQuery.cookie('userList', JSON.stringify(updatedUserList))
(and of course parse it when you're retrieving it.)

Categories