2
<ul>
    <a href="#Project">
        <li onclick="project()">Projects</li>
    </a>
</ul>

a tab appears and you can click on it, which runs a javascript function and also changes the url to www.demo.com#Project.

Is it possible when i give someone the link like www.demo.com#Project it loads the page and automatically runs function project()

EDIT SOLUTION

if(window.location.hash == "#Project") {
        setTimeout('project();', 1);
} 
else {
}

a timeout must be set so it loads the page first then execute function

6
  • 2
    You cannot have <li> directly as a child of <a> and also you cannot put <li> or any other block elements inside <a>. It's like putting a bottle inside water, as a container. Commented Dec 2, 2016 at 6:43
  • @PraveenKumar I fixed up the example code Commented Dec 2, 2016 at 6:44
  • Still the same. Read my comment correctly. Commented Dec 2, 2016 at 6:45
  • You can do this <li><a></a></li> but not <a><li></li></a>. Check this Commented Dec 2, 2016 at 6:49
  • Are you going to have multiple links like this, with an anchor and a corresponding JS function? Commented Dec 2, 2016 at 7:12

5 Answers 5

5

No, script won't run if your give someone the URL. To achive this you should check whether window.location.hash equals '#Project' on page load.

Hope this helps

Btw. what Praveen Kumar mentioned is changing your code as follow:

<ul>
    <li onclick="project()">
        <a href="#Project">Projects</a>
    </li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

1

I assume you want this to work on several different anchors, so you could do something like this:

<ul>
    <li onclick="project()">
        <a href="#Project">Projects</a>
    </li>
</ul>

<script>
    function project() {
        alert('Function project called')
    };

    var elements = document.querySelectorAll("a[href='" + window.location.hash + "']")
    if (elements.length > 0)
    {
        elements[0].click();
    };
</script>

This code will make sure any hash anchor gets clicked when you navigate to the url with that hash.

Comments

0
the link will be for example "www.test.com#myproject" 
and "function project" will be run automatically

 <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
    <script>
    function project()
    {
      alert("hello");
    }    
    </script>
    </head>
    <body>
    <div id="myproject">
        <img src="test.jpg" onload="project()">
      <a>  sample text</a>
        <p>123456789</p>
        <ul>
          <li>one</li>
          <li>two</li>
          <li>three</li>
        </ul>
    </div>
    </body>
    </html>

Comments

0

A small example

If (window.location.hash === '#Project')  {
  project() ;
}

2 Comments

doesnt work also why have you done === shouldn't it be ==
if(window.location.hash == "#Project") { setTimeout('project();', 1); } else { // No hash } Is the correct way
0

Yes it is possible. You dont have to do <a href="#Project"> <li onclick="project()">Projects</li> </a> because it is a painful code. Try to do this:

  `<ul>
      <li>
          <a href="#" onclick="project()">Projects</a>
      </li>
  </ul>`

It basically run the function called project() when you click the Projects. Base on your code your just navigating an element that has an id of Project.


You can also use

<ul>
      <li>
          <a href="www.demo.com/projects.html">Projects</a>
      </li>
  </ul>

Then in projects.html run your script:

<script>

function project(){
  alert("I am your function.");
}

project(); //Call the function

</script>

5 Comments

but I dont want my website to have multiple pages.
<ul> <li> <a href="#Projects" onclick="project()">Projects</a> </li> </ul>
Try that one. Then let me know whats going on
The Same thing as i quoted. runs the function and make the url contain #Projects
No sir. Its not the same. Try to check your HTML structure. You must also consider the structure when developing.

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.