0

I'm a beginner with HTML-CSS-JS and I have this problem :

When I'm at page1.html of my website, I can sort the different article of the page by selecting a sub-menu of my navigation bar (for example sub-menu 1 is "sport" and when I click on it only the article tagged as "sport" are visible). Here is the function :

        <script style="text/javascript">
    <!--
        function sortNews(id){
            var elmt = document.getElementsByTagName("section");

            for(var i = 0; i<elmt.length; i++){
                if(id == "all")
                    elmt[i].style.display = "block";
                else if(elmt[i].className != id)
                    elmt[i].style.display = "none";
                else
                    elmt[i].style.display = "block";
            }
        }
    // -->
    </script>

When I am at page2.html I can also select the submenu of page 1 via the navigation bar (it means when my mouse go over the menu "news" the sub-menu appear and I can click directly on "Sport"). My question is : can I load the page1.html AND run the function above without another click of the user ? (So the page of news with only the "sport" article will be loaded)

Thank you in advance ! Best regards

Laurent
  • 1,710
  • 4
  • 25
  • 46
  • 1
    not sure what you are asking. can you please modify the question and make it clear of what you are asking for? – MaVRoSCy Aug 03 '12 at 08:37

2 Answers2

3

Call your function in body's onload attribute of your page1.html

<body onload="sortNews(id);">

But you will need something to get the id value.
You could use php and more specifically $_GET to do so.

(In your page2.html the link will be like :

<a href="page1.php?id=sport>Sports News !</a>

and in page1.php

<body onload="sortNews(<?php echo $_GET['id']; ?>);">

)

Jerska
  • 11,722
  • 4
  • 35
  • 54
  • Thank you for this ! The only problem is that I don't know PHP (for the moment) I've started with HTML than CSS and at last JS ! Next step is PHP ! Is there a solution without PHP ? – Laurent Aug 03 '12 at 09:14
  • Well, you will need to analyze the query string with js. (The query string is where the GET elements are taken by PHP, everything that comes after "page.php?". That's possible, [as you can see here](http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript), but this will be your part of the job. – Jerska Aug 03 '12 at 09:20
  • OK I'll try that ! Or learn PHP maybe ! Anyway, thank you ! ++ – Laurent Aug 03 '12 at 09:48
2

I'm not exactly sure what your asking, do you want a function to run when you load the page? If so: http://javascript.about.com/library/blonload.htm

Andy
  • 14,427
  • 3
  • 52
  • 76