Click here to Skip to main content
15,921,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to swap HTML Code/Items on refresh or page load.

My page has multiple HTML items like.

Item 1
Item 2
Item 3
......
Item 10

when I refresh the page it should swap the items like

Item 3
Item 2
Item 10
......
Item 1


My listings are in HTML I am not using any database or programming.
Posted
Comments
Gerd Wagner 3-Apr-14 7:09am    
Of course, the simplest approach for solving this problem is using JavaScript. But your question is underspecified: of which element type are your "items" (e.g. select/option elements?), and what is your criterion for ordering them?
enhzflep 4-Apr-14 0:13am    
Have a look into localStorage. It lets you store stuff (up to 5MB) in the browser. It is persistent and update-able.
You could easily store the number of times the page has been loaded, before then going on to calculate the first element to display.

Perhaps something like this small snippet:
------------------------------------------------------------------------------------------
window.addEventListener('load', onDocLoaded, false);

function onDocLoaded()
{
// page never loaded before - have to add 'timesLoaded' to the localStorage database
if (localStorage.timesLoaded == undefined)
localStorage.timesLoaded = 1;

// page has been loaded previously, 'timesLoaded' already exists, just increment it.
else
{
var numTimes = localStorage.timesLoaded;
numTimes++;
localStorage.timesLoaded = numTimes;
}
var txt = document.createTextNode("Page has been loaded " + localStorage.timesLoaded + " times.");
document.body.appendChild(txt);
}
------------------------------------------------------------------------------------------
Note: There are different methods by which you can access localStorage. Have a read up on it to ensure you use the best one - i.e the one that is supported by all/most browsers. I've simply used one which works in Chrome - no idea which, if any, browsers will fail with my code.

1 solution

To swap the code via php would require a trip back to the server (such as via AJAX). Since you specify you've no database on the server (related to this), I'll presume you also mean no text files or anything else that's used as a data source.

With that being said, everything you need is on the client and therefore you should be using javaScript to do the work.

As for even that, you've two primary approaches, the better one to select depending upon the types of HTML elements these are:

  1. Swap values
  2. Swap Locations


One may nuance this to other levels, such as capturing and replacing 'innerHTML', but that's the sensible way to handle this.

(As an aside: I prefer php to javaScript when I do have a choice - it's just more fun).

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900