Javascript function execution on link click?

I have a link that when a user clicks on it, it loads another page as usual, but also performs a JS function that automatically fills a specific text block on this / strong>. Is it better to use jquery or javascript for this? How can I do this using one of them?

+4
source share
2 answers

You cannot do this on the original page.

This is a safety feature. Imagine if you wrote a JS function that went to an online banking page and automatically filled out a bank transfer using the user's current cookie. That is why you cannot.

If you are managing another page, then you can use the following sequence:

  • Save data on the server;
  • Go to the new JS redirect page;
  • A new page is loaded from the server;
  • When loading data, data saved from the server is retrieved and used to fill the text field.

So it can be done from the server, but only if you save it. The only way to do this is to use Ajax.

Alternative approach:

  • Instead of redirecting JS, send the page back to the server;
  • The server saves all the data it needs;
  • The server sends an HTTP redirect to a new page;
  • The new page uses the saved data to create a new page with a filled text field.
+1
source

At the end of the script, add return false; . This will force the page to run the script without redirecting the page.

Edit: (after viewing your version).

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, it doesn’t matter if you use plain javascript or use jquery while you are happy with the result.

And that you say that you have successfully manipulated the page of the redirect page ... I do not see how this is possible.

+1
source

Source: https://habr.com/ru/post/1303182/


All Articles