How to use the file name of a web page inside the code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tur130
    New Member
    • Jul 2010
    • 16

    How to use the file name of a web page inside the code?

    The following code is in a file called Dog_features.ht m

    Code:
    document.getElementById('MyDiv').innerHTML = "<iframe src='Dog_Page1.htm'></iframe>";
    Is there a way to replace the word Dog in the code with a variable which resolves to the first word in the page's file name? e.g.

    Code:
    document.getElementById('MyDiv').innerHTML = "<iframe src='Animal_Page1.htm'></iframe>";
    So if the 2nd snippet is in a file called 'Cat_features.h tm' then the file 'Cat_Page1.htm' would be loaded into the iframe.
  • tur130
    New Member
    • Jul 2010
    • 16

    #2
    Ok I got this far on my own...

    Code:
    function getFileNameAnimal() {
    //this gets the full url
    var url = document.location.href;
    //this removes the url end after "_"
    url = url.substring(0, (url.indexOf("_") == -1) ? url.length : url.indexOf("_"));
    //this removes everything before the last slash in the path
    url = url.substring(url.lastIndexOf("/") + 1, url.length);
    //return
    return url;
    }
    Will get the bit of the file name I need...almost there...

    Comment

    • tur130
      New Member
      • Jul 2010
      • 16

      #3
      Solved it - for future ref posting here...

      Code:
      <div id="MyDiv">This Text gets replaced with the appropriate animal_Page1.htm file based on the file name of this file</div>  
                     
      <script>
      function getPage1() {
      var Animal = getFileNameAnimal();
      document.getElementById('MyDiv').innerHTML = "<iframe src='"+Animal+"_Page1.htm'></iframe>";
      }
      </script>                 
                       
      <input id="btn3" value="Get Page 1" 
                       onclick="getPage1()" type="button"/>
      This code uses the function I posted earlier.

      Comment

      • drhowarddrfine
        Recognized Expert Expert
        • Sep 2006
        • 7434

        #4
        You should ask javascript questions on the javascript board.

        Comment

        • tur130
          New Member
          • Jul 2010
          • 16

          #5
          Originally posted by drhowarddrfine
          You should ask javascript questions on the javascript board.
          Will try harder to get the right board next time.

          That said my requirement is actually javascript agnostic. If there is a way to do this without javascript that would be even better.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            Originally posted by tur130
            Will try harder to get the right board next time.

            That said my requirement is actually javascript agnostic. If there is a way to do this without javascript that would be even better.
            There is, but then you need server side script (like PHP for example). If you are using pure HTML/CSS, there is no execution, only display. What you are asking for is something only a script can achieve and then you can choose server side or client side.

            Comment

            • tur130
              New Member
              • Jul 2010
              • 16

              #7
              I'll bow to your experience here.

              That said I've seen some clever tricks with CSS that make it 'look' like some script is at work where in fact there isn't. I guess my point is that a solution can come from an unexpected angle and posting in a particular technology board kind of implies that you want the solution to use that particular technology.

              Perhaps there should be a technology agnostic question board? Where you can just post your requirements and constraints and then see if you can get some inspired ideas that otherwise might not have been realized.

              On this subject - is there any other technology (other than HTML, JS, CSS) hidden in bowers, perhaps not commonly used any more that is worth reading up on?

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                Originally posted by tur130
                I'll bow to your experience here.

                That said I've seen some clever tricks with CSS that make it 'look' like some script is at work where in fact there isn't. I guess my point is that a solution can come from an unexpected angle and posting in a particular technology board kind of implies that you want the solution to use that particular technology.

                Perhaps there should be a technology agnostic question board? Where you can just post your requirements and constraints and then see if you can get some inspired ideas that otherwise might not have been realized.

                On this subject - is there any other technology (other than HTML, JS, CSS) hidden in bowers, perhaps not commonly used any more that is worth reading up on?
                No need to bow, we're all students.

                I am not aware of any "hidden" technology. Browsers are actually very simple, they display what they're sent. More advanced browsers can deal with client side scripting (javascript, AJAX), but generally they're not designed to do much more. That is going to change very soon with "the cloud" but for now Bytes generally has all the technologies worth learning. Having said that, I believe ASP can do some things that others can't, simply because it's made from Microsoft.

                With regards to the technology agnostic board: We kinda do have one for Software development, but it's not used in website building. I imagine if you get to the stage of being able to switch between all technologies, you won't need to ask a "technology agnostic question" because you will know which language is best for that situation. However, if you are like most, you will have your preferences, and a little bit of some others to fill in the gaps your preferences cannot do. What I believe Bytes prefers to go for is users to have some idea on languages before they get here. Obviously we can point you to a more appropriate language if you are clearly in the wrong language, but if you don't even know what the different languages are (or a few in particular), you are not in any good position to ask an intelligible question anyway.

                I have gone to the Java forums and asked a silly, uneducated question like: Should I use Java or C++ for THIS. The response was very limited, because the truth is, most languages can do most things, and it comes down to your experience, background, future plans, time available, etc. These cannot be captured in a simple post to ask for a technology agnostic question.

                So to sum up, while I am not opposed to the idea, I do not seeing it being greatly helpful, and could result in too many "general/uneducated questions".

                [/rant]

                Comment

                • tur130
                  New Member
                  • Jul 2010
                  • 16

                  #9
                  You guys are very responsive and know your stuff - cheers.

                  Comment

                  Working...