damon
damon

Reputation: 2897

Is it possible to link directly to a JavaScript onclick function?

I have a page where you can load content via JavaScript from an onclick function. Is it possible to link to the page where it automatically loads one of the functions?

HTML:

<nav>   
            <p id="bout" onclick="bout()">About Us</p>
            <p id="mish" onclick="mish()">Our Mission</p>
            <p id="team" onclick="team()">The Team</p>
            <p id="how" onclick="how()">How It Works</p>
            <p id="poli" onclick="poli()">Policies</p>
        </nav>

        <div class="actual">

            <div id="about">
            <h2>About Us</h2>
            <p>We are a conglomerate of hoodlums.</p>
            </div>

 </div><!-- end actual -->

JS:

    function bout() {
        document.getElementById("about").innerHTML= '<h2>About Us</h2><p>We are a conglomerate of hoodlums.</p>';
    }
function mish() {
    document.getElementById("about").innerHTML = '<h2>Mission</h2><p>Our mission is to rid the world of dust bunnies.</p>';

I would like to be able to link directly to this page with mish() loaded instead of the default HTML. Is this possible?

Upvotes: 1

Views: 481

Answers (2)

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

You're looking for a url hash to maintain state.

For example:

function bout() {
     document.getElementById("about").innerHTML= '<h2>About Us</h2><p>We are a conglomerate of hoodlums.</p>';    
     document.location.hash="bout";
}

Then when the page loads, look at the value of document.location.hash, if it equals "bout", call the bout function.

Upvotes: 0

drz
drz

Reputation: 992

Yes, use a hash tag and call the function from there.

a hash url would look like this:

http://myserver.com/mypage.htm#about

Create a js function that calls the correct function when you load the page, for example:

function onloaded(){
    hash = document.location.hash;
    switch (hash):
        case 'mish':
        mish();
        break;
    ...
}

Upvotes: 4

Related Questions