Blynn
Blynn

Reputation: 1411

Click on page load to get a url

The code below is supposed to get the new url as root while the page loads. I tried targeting the class as .click and that didn't work as well. Any help would be appreciated.

    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>

    <script>
    $(document).ready(function() {
        $("a").trigger('click');
    });
    </script>

    <a href="/" class="click">&nbsp;</a>

Upvotes: 0

Views: 154

Answers (2)

Blazemonger
Blazemonger

Reputation: 92893

You can use trigger('click') to trigger the JavaScript code for a click event. However, for security reasons, you cannot use it to simulate a mouse click that would actually open a link. You must do that by assigning to window.location directly:

$('a.click').trigger('click'); // everything but changing the URL
window.location = $('a.click').attr('href'); // change the URL

Also, please read this.

Upvotes: 2

Surreal Dreams
Surreal Dreams

Reputation: 26380

You could just use this and skip jQuery and the a tag altogether:

<script>
    window.location.href = '/';
</script>

This will set the browser's address to the site root and take you there without having to simulate any clicks or even wait on document ready. Also, no jQuery needed.

Upvotes: 5

Related Questions