How to run a script on a page accessed using href

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kresimirt
    New Member
    • Dec 2009
    • 3

    How to run a script on a page accessed using href

    Hi,
    I am trying to enhance the functionality of my web site which is hard with my limited knowledge of web developing.
    The idea is the following:
    On the main web page there is a show_hide script that looks like this:
    Code:
    <script> function show_hide(the_layer)
    {
    if(document.getElementById(the_layer))
    {
    if(document.getElementById(the_layer).style.display == 'none')
    {
    document.getElementById(the_layer).style.display = 'inline';
    }
    else
    {
    document.getElementById(the_layer).style.display = 'none';
    }
    }
    }
    On the page there are a number of statements like:
    Code:
    <a href="javascript:show_hide('sectionx');">Sectionx</a>
    which open particular section in the text.

    On another page I have links to the main page:
    Code:
    <a href="mainpage.htm">Main page</a>
    which open the main page but the visitor has to locate the section of interest by himself and click on it to open it.
    I would like to have direct links to sections, but I do not know how to open the main page and run the show_hide scipt in a sigle href statement. Something like:
    Code:
    <a href="mainpage.htm ????javascript:show_hide('section4');">Main page - Section4</a>
    Is it possible to accomplish this?

    Thanks in advance
    KT
    Last edited by gits; Dec 29 '09, 04:42 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    when you open a new page the page itself should include the script and call the method in its onload-handler ... unless the new page is opened in an iframe or similar item on a base-page ...

    kind regards

    Comment

    • kresimirt
      New Member
      • Dec 2009
      • 3

      #3
      I modified both pages.
      On the page that links to the mainpage I modified the href statement:
      <a href="mainpage. htm?section4">M ain page - Section 4</a>
      with the idea to pass the string 'section4' to the mainpage.
      The onload function of the main page has to receive the information that I want to open section 4. Therefore, on the mainpage in the body tag I added the onload part in the form:
      onload="show_hi de_call()"
      I also defined a function:
      Code:
      function show_hide_call()
      {
      variable=???????????
      javascript:show_hide(variable);
      }
      The question is: what to put in the place of questionmarks?

      Sorry for the trouble, but, basically, I have zero knowledge of javascript and passing values using href statement. I googled the web, found some probably usefull source codes but was unable to understand and use them.
      Thanks

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        in this case you might use the location reference of the window:

        Code:
        function show_hide_call() {
            var myVar = location.search;
            show_hide(myVar);
        }
        you might improve that by 'regExing' the params out like:

        Code:
        var myVar = location.search.match(/[^?]+$/)[0];
        and to make it more robust ... it could look like:

        Code:
        function show_hide_call() {
            var action = location.search.match(/[^?]+$/);
            var myVar = action != null ? action[0] : 'aDefaultValue';
            show_hide(myVar);
        }
        kind regards

        PS: you don't ever need the javascript: in href's or scripts.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Also look into using #, so that it can work without JavaScript. If JavaScript is enabled, use location.hash to retrieve the value.

          Comment

          • kresimirt
            New Member
            • Dec 2009
            • 3

            #6
            Gits, thanks a lot.
            I used the simplest method and it works fine. Although, I had to pull out a substring from myVar to eliminate the leading questionmark.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              that is equivalent to using the above shown regEx ... :) ... glad to hear you got it working :)

              kind regards

              Comment

              Working...