Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    84
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Arrow Dynamic insertion of information from URL

    Hey all, i could really use some help in making life easier if any one can help.

    Im gonna try my best and explain what i want but please excuse my lack of knowledge!


    What i want to achieve is to use one page as a template that can take information from the url and use it to produce the result i want for example

    if the template page is template.html ad it looked something like this:


    Code:
    <title> + title + </title>
    </head>
    
    <body>
    <a href="http://www.mywebsite.com/ + folder + / + page +">Click Here</a>
    </body>
    </html>

    The link to the page could look something like this:

    template.html?folder=football&page=united.html&title=United Scores


    this would result in the template page rendering as:

    Code:
    <title>United Scores</title>
    </head>
    
    <body>
    <a href="http://www.mywebsite.com/football/united.html">Click Here</a>
    </body>
    </html>

    If anyone can tell me how i can achieve this i would be extremely grateful.

    Thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    Not hard.

    I would do it thus:
    Code:
    <html>
    <head>
    <title>Generic Title</title>
    <script type="text/javascript">
    // parse the querystring and create a dictionary of terms for later use:
    var qs = [ ]; // where the dictionary goes
    
    if ( location.search.length > 1 )
    {
        var pairs = location.search.substring(1).split("&");
        for ( var p = 0; p < pairs.length; ++p )
        {
            var pair = pairs[p].split("=");
            qs[pair[0]] = unescape( pair[1] );
        }
    }
    
    // do the replacements *after* page is loaded, for simplicity:
    function doReplacements( )
    {
        document.title = qs["title"];
    
        document.getElementById("heading").innerHTML = qs["title"];
    
        var link = document.getElementById("link1");
        link.href = "http://www.mywebsite.com/" + qs["folder"] + "/" + qs["page"] + ".html";
        link.innerHTML = qs["page"];
    
        // and of course you could do any other replacements, too.
    } 
    window.onload = doReplacements;  
    </script>
    </head>
    <body>
    <center><h1 id="heading">Generic Heading</h1></center>
    <br/><br/>
    <a id="link1">Generic Link</a>
    <br/><br/>
    </body>
    </head>
    Then test that using
    Code:
    template.html?folder=football&page=United&title=United Scores
    (Notice that I left the ".html" off of the page, so that I could use it for two purposes. You can change that as you want, of course.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    84
    Thanks
    9
    Thanked 0 Times in 0 Posts
    This looks wonderful Old Pedant!

    I had a little play with it and it looks perfect. Im gonna try and incorporate it into my site and with fingers crossed it will do the job i wanted it to, as for what i asked, you hit the nail on the head. If you dont mind, i may have to post back here if i get stuck, but for all future readers, this is Perfect!

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    84
    Thanks
    9
    Thanked 0 Times in 0 Posts
    is there any way to keep echoing the url as a url only?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,655 Times in 4,617 Posts
    which url?

    This one?
    Code:
    "http://www.mywebsite.com/" + qs["folder"] + "/" + qs["page"] + ".html";
    Sure. Just make a minor special case out of it.
    Code:
    <script type="text/javascript">
    // parse the querystring and create a dictionary of terms for later use:
    var qs = [ ]; // where the dictionary goes
    
    if ( location.search.length > 1 )
    {
        var pairs = location.search.substring(1).split("&");
        for ( var p = 0; p < pairs.length; ++p )
        {
            var pair = pairs[p].split("=");
            qs[pair[0]] = unescape( pair[1] );
        }
    }
    var targeturl = "http://www.mywebsite.com/" + qs["folder"] + "/" + qs["page"] + ".html";
    And then just use the variable targeturl any place on the page where you need that.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •