0

I have a page for redirect purpose, I want to redirect user to another page once it hits this page:

<!DOCTYPE html>
<html>
<head>
    <title>Open App</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            alert("test"); //this shows up.
            $('#openApp').click(); //this button is not clicked.
        });
    </script>
</head>
<body>
    <a href="http://www.google.com" id="openApp">redirect</a>
</body>
</html>

I want the redirect happen once this page is load. But the click function is not fired. I have to click the link by myself to redirect to google.com.

laconbass
  • 17,080
  • 8
  • 46
  • 54
Carrie
  • 480
  • 3
  • 10
  • 25

3 Answers3

0

It's not possible to trigger a click event without any user interaction. In other words, if the function being called wasn't called from a user interaction, the click event will not get triggered.

In your case, simply update the window.location attribute:

window.location = 'http://www.google.com';
Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
  • you mean remove click function, add `window.location = ('http://www.google.com', '');` ? It doesn't work, it keeps refreshing the page. – Carrie Jan 28 '16 at 16:49
0

I normally wouldn't give you an alternative as an answer, but looking at your comments, it looks like you're just trying to redirect.

You can use window.location.replace('http://www.google.com'); for that.

$(document).ready(function () {
  window.location.replace('http://www.google.com');
});

See also, this answer.

Community
  • 1
  • 1
Antiga
  • 2,264
  • 1
  • 20
  • 28
0

try this fiddle mate, added script is

 $(document).ready(function () {
        //this shows up.
        $('#openApp')[0].click(); //this button is not clicked.
    });
Siddharth
  • 859
  • 8
  • 16