I have a link, that when a user clicks on it, it loads a different page as normal but also executes a JS function that autofills a specific text-box on that different page. Is it better practice to use Jquery or Javascript to do this? How can I do this using either one of them?
-
How would it load a page(that it's leaving?) and fill a textbox? Is it an iframe, something else?Nick Craver– Nick Craver2010-03-05 01:39:31 +00:00Commented Mar 5, 2010 at 1:39
-
sorry was unclear, the link is to an external page. It needs to fill a textbox on said external pageAlex– Alex2010-03-05 01:41:03 +00:00Commented Mar 5, 2010 at 1:41
-
You have control over this separate page? It sounds like you want a post here, not a link/getNick Craver– Nick Craver2010-03-05 01:43:25 +00:00Commented Mar 5, 2010 at 1:43
-
yes I do. I own the other page as well.Alex– Alex2010-03-05 01:45:36 +00:00Commented Mar 5, 2010 at 1:45
-
But I only want the JS to execute on the external page when accessed from a certain link.Alex– Alex2010-03-05 01:46:04 +00:00Commented Mar 5, 2010 at 1:46
2 Answers
You can't do this from the source page.
It's a security feature. Imagine if you wrote a JS function that went to an online banking page and auto-filled a bank transfer using the user's current cookie. That's why you can't.
If you control the other page then the sequence you can use is:
- Save data to the server;
- Go to the new page with a JS redirect;
- The new page is loaded from the server;
- While loading th epage the data that was saved from the server is retrieved and used to populate the text box.
So it can be done from the server but only if you save it there. The only way of doing that is using Ajax.
An alternative approach is:
- Instead of a JS redirect, submit the page back to the server;
- The server saves whatever data it needs to;
- The server sends back an HTTP redirect to the new page;
- The new page uses the saved data to construct the new page with the populated text box.
2 Comments
At the end of the script add return false;. This will make the page run the script without redirecting the page.
Edit: (after saw your edition).
Is it better practice to use Jquery or Javascript to do this? How can I do this using either one of them?
jQuery is a javascript library, this it doesn't matter if you use plain javascript or use jquery as long as you happy with the result.
And about what you say that you successfully manipulated a page fro the redirecter page... I don't see how it possible.