1

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?

2
  • I would like to be able to link directly to this page with mish() loaded instead of the default HTML????explain Commented Jul 18, 2013 at 19:26
  • do you mean you would like to open the page with a given parameter? Commented Jul 18, 2013 at 19:29

2 Answers 2

4

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;
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

damn, you type faster.
A better way to do it than a switch is to have an object with the hash functions. Then you can do: if(hashFunctions.hasOwnProperty(hash)) { hashFunctions[hash](); }. It's easier to maintain since you don't have to keep up with a switch that could get quite long if you have a lot of possible hash ids.
agreed, but I was trying to keep the example simple.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.