[SalesForce] Pass apex value from one page to another

I have an outputLink like this:

<apex:outputLink value="/apex/NewPage?id='xxxcccxxx'>
    <div class="link_class">
        <strong>Link</strong>
    </div>
</apex:outputLink> 

I want to pass an apex value through with this link to the next page, I'd like to pass it through s a hidden value if that's possible.

{!my_val}

And then catch it on the next page and throw it into a div.

I can't pass it in the URL as I need the URL to remain as it is.

Only using JS, html or APEX, is this possible?

Best Answer

If both pages use the same controller and extensions, you can actually pass the state of the page from the first to the second. You'd need to change the code from an apex:outputLink to a apex:commandLink to make this happen, though.

If you don't want to share the view state across pages (e.g. you need different controllers), you can also use localStorage or sessionStorage to store the variable in the browser's memory cache while you travel between pages. This will require cooperation on both pages, as the former will need to set the value (localStorage.setItem('somekey', 'value')), then retrieve it on NewPage (localStorage.getItem('somekey')). You can also use document.cookie for small values (less than about 4kb) for broader browser compatibility.

If you have no control over NewPage, then you're pretty much out of luck. You need a POST action for the variable to persist invisibly, which means the receiving page needs to be able to understand this POST request. Using apex:commandLink would be the easiest way to achieve your goal, especially if you can design both pages to use the same controller.