1

I have the following HTML code :

<div class="action-button">
    <a href="/wp-login.php?loginFacebook=1&amp;redirect=http://blabla.com" class="MyButton">Hello
    </a>
</div> 

Is it possible to call a javascript function (stored in a file) while redirecting instead of redirecting to a specific URL ?

The javascript file is in the same domain as the rest.

Thank you.

Thomas Carlton
  • 5,344
  • 10
  • 63
  • 126
  • 1
    Why down voting ? What's wrong with someone not expert asking basic question ?? – Thomas Carlton Feb 02 '16 at 23:03
  • Yes, if you have a server-side javascript such as [node.js](http://blog.modulus.io/build-your-first-http-server-in-nodejs) or others.. – Daniel Feb 02 '16 at 23:06

3 Answers3

1

You can do it with a query string. for example

 <a href="http://blabla.com/mypage.html?callfunction=1">Hello</a>

then on the page it redirects to (mypage.html) you'd do something like

<script>
if (getParameterByName('callfunction') == 1)
    callMyFunction1();
</script>

You could also pass parameters via querystrings.

For more info on querystrings see-- How can I get query string values in JavaScript?

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
Community
  • 1
  • 1
Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • Actually the first part of the code is important. Because it opens a session using facebook credentials ( – Thomas Carlton Feb 02 '16 at 23:32
  • why not redirect to a client side html first and then make your ajax call to your server. but if you must go server first you could still extract a querystring from it and then run it in client code. if you're the server you're building the client code. – Shai UI Feb 02 '16 at 23:41
0

Nope, the function needs a context to run in. That would be a website. Although you could create a dummy page that does nothing but execute the function you're interested in using.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
0

Include the JavaScript upfront, then call the function from the submit handler

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217