Results 1 to 3 of 3

Thread: [RESOLVED] Dynamically Creating Links

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    3

    Resolved [RESOLVED] Dynamically Creating Links

    OK, so I have a simple search form (just a text input box and a submit button). When I click the submit button, I'm using a script to create a whole bunch of links based off the input of the text box. For example

    Code:
    function searchit(){
    var query = document.form.query.value;
    var url1 = "google.com?q="+query;
    var url2 = "whatever.com?search="+query; }
    What I want to be able to do is launch a window which has these links listed saying something like:
    Click here for Google results
    Click here for Whatever results

    and have that text link to the urls that the javascript has created.

    Can I just do something like:
    Code:
    <a href='url1'>Click here for Google results</a>
    Can anyone help me with this?

  2. #2
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    285

    Re: Dynamically Creating Links

    Hi there tinders,

    have a look at this example, it may suit your requirements...
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    
    <title></title>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <style type="text/css">
    #container {
        width:260px;
        padding:6px 0;
        margin:auto;
        border:3px double #003;
        background-color:#ccf;
        text-align:center;
     }
    input {
        width:230px;
        margin:4px 0;
     }
    .but {
        background-color:#003;
        color:#ccf;
     }
    </style>
    
    <script type="text/javascript">
    
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      }
     }
    
    function init(){
    
       df=document.forms[0];
       inp=df.elements;
       k=0;
       url=['http://www.google.co.uk/search?q=',
            'http://uk.search.yahoo.com/search?p=',
            'http://www.answers.com/main/ntquery?s='
           ];
       df[0].focus();
    
    for(c=0;c<inp.length;c++) {
    if(inp[c].className=='but'){
       inp[c].id=k++;
    inp[c].onclick=function() {
       checkForm(this.id);
        }
       }
      }
     }
    
    function checkForm(n) {
    
       term=df[0].value;
    if(term=='') {
       alert('please select "search keyword"');
       df[0].focus();
       return;
     }
       location.href=url[n]+term;
     }
    
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div id="container">
      <input type="text">
      <input class="but" type="button" value="search google">
      <input class="but" type="button" value="search yahoo">
      <input class="but" type="button" value="search answers">
    </div>
    </form>
    
    </body>
    </html>
    


    ~ the original bald headed old fart ~

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    3

    Re: Dynamically Creating Links

    Thanks for that. It's not quite what I was after, but I've learnt a bit by studying the code regardless.

    I actually ended up writing a script which initialises all the URL's I need as strings (var1, var2 for example), and then I called a popup, and used the document.write method to populate the window.

    The links then became: <a href='+var1+' target="_blank">Text</a>

Posting Permissions

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



Click Here to Expand Forum to Full Width