0

how to First Redirect to particular URL and after that execute function logic in JavaScript ?

i have try following code. JavaScript :

<script type="text/javascript">
    function onHrefClick(varHref) {
        window.open(varHref, '_blank');
        //here code
    }
</script>

HTML:

 <a href="#" onclick="onHrefClick('www.google.com')">Google</a>

But it always first execute my code and then it will redirect to URL.

Update :

i try this also it will open first popup window then redirect on page. it will wait for redirect till i am not click on Popup OK button.

<script type="text/javascript">
function onHrefClick(varHref) {
    window.open(varHref, '_blank');
    alert(varHref);
}

I want to redirect on external link so here no any parent or child concept

Sagar Patel
  • 4,993
  • 1
  • 8
  • 19

2 Answers2

0

You can do so:

            function myFunction() {
                //my code here
                $('a').css('color', 'red');
            }
            function redirectOnHrefClick(varHref) {
                window.open(varHref, '_blank');
                myFunction();
            }
<a href="#" onclick="redirectOnHrefClick('www.google.com')">Google</a>

This work. I tested here.
I hope to help.

Ricardo Mota
  • 161
  • 5
0

If you want to redirect to the page and execute the script in that page, there are 2 things that you can do:

  1. If the webpage is controlled by you, please go ahead and write the code in $(Document).Ready(function() {} ) function.
  2. If the website is controlled by a external source like www.google.com please go ahead and check if there is any api easing it.
Asteroid
  • 57
  • 1
  • 11