Johnson
Johnson

Reputation: 818

jQuery function equal to href=#

Is there a jquery function that can make you go to #name, like you can if you make a link to href="#name", so I could on document ready directly go to #name

Upvotes: 2

Views: 630

Answers (4)

andres descalzo
andres descalzo

Reputation: 14967

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
<a id="goto" href="#this">go to this</a>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="this">this</div>
</body>
</html>
<script type="text/javascript">
    $(function(){

        $("#goto")[0].click();

    });
</script> 

Or

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<div id="this">this</div>
</body>
</html>
<script type="text/javascript">
    $(function(){

        $('<a href="#this">go to this</a>')[0].click();

    });
</script> 

Upvotes: 0

Gregg B
Gregg B

Reputation: 13727

While a bit overkill you can try the scrollto plugin:

Scrollto

Upvotes: 1

Adam
Adam

Reputation: 44949

There is JavaScript:

window.location.hash = "#name"

https://developer.mozilla.org/en/DOM/window.location

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630569

You can just change the location.hash, like this:

$(function() {
  window.location.hash = '#name'; //works with or without the # included
});

Though if you just hyperlink to the page, this is built-in browser behavior, for example going to http://site.com/page.html#name would have the same scroll-on-load effect.

Upvotes: 5

Related Questions