Question

I have a link on one page which looks like this:

http://www.domain.com/sample-link#product_id

and on the other page (sample-link), I have this input field:

<input type="text" name="name" value="name" />

So, when I click the link from the first page, I want to open the "sample-link" page, and autofill the name field with the "product_id" text. Any ideas how can I make this?

Thanks in advance.

Était-ce utile?

La solution

You'll just have to add a tiny Javascript snippet:

if (document.location.hash)
    document.getElementById('testbox').value = decodeURIComponent(document.location.hash.substr(1));

For obvious reasons you'll have to adjust the id of the text box. It gets a bit more complicated in case you'd like to pass more than one value.

The call to decodeURIComponent() is optional, but required in case you're passing characters like spaces or non-alphanumerical stuff (just to be sure).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top