Web Analytics Made Easy -
StatCounter I need help with some javascript: - CodingForum

Announcement

Collapse
No announcement yet.

I need help with some javascript:

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • I need help with some javascript:

    I am trying to replace [[x y z]]
    with
    <a href="http://en.wikipedia.org/wiki/x_y_z">x y z</a>".
    , similarly i am trying to replace [[x#y]]
    with
    <a href="http://en.wikipedia.org/wiki/x#y">x#y</a>
    -----
    here is what I have so far:
    <html>
    <head>
    <title>Project.Link</title>
    </head>
    <body>
    <script type= "text/javascript">
    var input = window.prompt("Enter Text Here");
    for(i=0;i<=input.length;i++)
    {
    var openbrackets = input.indexOf("[[");
    var closebrackets = input.indexOf("]]");
    var linkstart = openbrackets+2;
    var linkend = closebrackets--;
    var link = input.substring(linkstart, linkend);
    }
    for (i=linkstart;i<=linkend;i++)
    {
    if (link.charAt(i) == " ")
    {
    var replace_ = link.replace(" ", "_");
    }
    }
    for (i=0;i<=input.length;i++)
    {
    var replaceo = input.replace("[[", '<a href="http://en.wikipedia.org/wiki/'+replace_+'">');
    var replacec = input.replace("]]", '</a>');
    }
    var sentence = replaceo.concat(link);
    var tsentence= document.write(sentence.concat(replacec));
    </script>
    </body>
    </html>

    ------

    Can anyone help me? Where is my code wrong?!?!

  • #2
    See if this helps, although I do not see where the square brackets come into it

    Code:
    <script type="text/javascript">
    inputStr=window.prompt("Enter Text Here");
    
    inputStr=inputStr.replace(/\[/g,"")
    inputStr=inputStr.replace(/]/g,"")
    
    inputStr2=inputStr.replace(/\s/g,"_")
    
    outputStr='<a href="http://en.wikipedia.org/wiki/'+inputStr2+'">'+inputStr+'</a>'
    
    document.write(outputStr)
    
    </script>
    Last edited by Mr J; Oct 6, 2006, 03:27 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

    Comment

    Working...
    X