using variable from javascript function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jon

    using variable from javascript function

    I would like to use the variable that the javascript creates as it
    parses the URL and be able to use this variable in other links on the
    same page. notice how link to page 2 has the token but the other links
    do not. Can I have one javascript in the heading and then call it up
    multiple times in the html to make my links (i.e.
    page3.html?refe r=google) I have the following webpage.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="content-type"
    content="text/html;charset=is o-8859-1">
    <title>Untitl ed Page</title>
    <script language="JavaS cript">
    //<![CDATA[
    function parseSearchStri ng()
    {
    var pairs=unescape( location.search .substring(1).r eplace(/\+/g,"
    ")).split(' &');
    for (var i=0;i<pairs.len gth;i++){
    var pair = pairs[i].split('=');
    this[pair[0]]=pair[1];
    }
    }
    var search = new parseSearchStri ng();

    //this will output all the passed variables

    for (o in search)
    document.write( '<a href="page2.htm l?refer=' + search[o]
    +'">Page2</a>');
    </script></head>

    <body bgcolor="#fffff f">
    <p></p>
    </body>

    </html>

    Thanks.

    Jon Lanclos
    jon@corporates. com
  • Michael Schmitt

    #2
    Re: using variable from javascript function

    Hi jon,
    I tested your script and it worked well, but keep in mind that you overwrite
    the content of your page, if you use document.write( ).

    jon wrote:[color=blue]
    > I would like to use the variable that the javascript creates as it
    > parses the URL and be able to use this variable in other links on the
    > same page. notice how link to page 2 has the token but the other links
    > do not. Can I have one javascript in the heading and then call it up
    > multiple times in the html to make my links (i.e.
    > page3.html?refe r=google) I have the following webpage.
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    > <html>
    > <head>
    > <meta http-equiv="content-type"
    > content="text/html;charset=is o-8859-1">
    > <title>Untitl ed Page</title>
    > <script language="JavaS cript">
    > //<![CDATA[
    > function parseSearchStri ng()
    > {
    > var pairs=unescape( location.search .substring(1).r eplace(/\+/g,"
    > ")).split(' &');
    > for (var i=0;i<pairs.len gth;i++){
    > var pair = pairs[i].split('=');
    > this[pair[0]]=pair[1];
    > }
    > }[/color]

    To add a new '<a>' to your page, you will have to write a function, that is
    called _after_ the page is completely loaded.
    [color=blue]
    > var search = new parseSearchStri ng();
    >
    > //this will output all the passed variables
    >
    > for (o in search)
    > document.write( '<a href="page2.htm l?refer=' + search[o]
    > +'">Page2</a>');[/color]

    function createLinks() {
    var search = new parseSearchStri ng();
    var b = document.getEle mentsByTagName( "body")[0];
    for (o in search) {
    var a = document.create Element("a");
    a.href = 'page2.html?ref er=' + search[o];
    var t = document.create TextNode("This links to page2.html with the post variable refer set to: " + search[o]);
    a.appendChild(t );
    b.appendChild(a );
    var br = document.create Element("br");
    b.appendChild(b r);
    }
    }

    </script>
    [color=blue]
    > </script></head>
    >
    > <body bgcolor="#fffff f">[/color]

    and here we call the function to set up the links:
    <body style="backgrou nd-color:#ffffff" onload="createL inks();"
    [color=blue]
    > <p></p>
    > </body>
    >
    > </html>
    >
    > Thanks.
    >
    > Jon Lanclos
    > jon@corporates. com[/color]

    Tested with Mozilla 1.7 and IE 6.0 Sp1
    Of cause you can append your links to other elements too.

    hth, Michael

    Comment

    Working...