SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Creating links with javascript problem

    I'm having problem with the following code. It is supposed to create a specified number of page links that i will go onto get to sort search results into different "pages" (i'm actually just gonna be changing the innerHTML element) which can be individually be accessed by each individual links, ie. Page 1 link accesses content of search result page 1
    The code is:

    <html>
    <head>
    <script language="javascript">

    //idP is the id of the P tag
    //jsCode is the javascript function that i want the link to run
    //linkTxt is the text displayed of the link

    function newLink(idP, jsCode, linkTxt) {
    alert("running ...");
    document.getElementById(idP).innerHTML += "<a href='javascript:" + jsCode + "'>" + linkTxt +"</a> &nbsp;";
    }


    //idP is the id of the P tag
    //commonName is something like "Page" so every link will be named "Page" + their number.
    //num is the number of links required

    function loopLinks(idP, num, commonName) {
    var i = 0;
    while (i < num) {

    // ERROR: "alert();" of the following, tries to link to "javascript:alert();" but if you attempt to put
    //something in, like "alert('hi');" it tries to link to "javascript:alert(" and ignores the rest

    newLink(idP, "alert();", commonName + (i + 1));
    i++;
    }
    }
    </script>
    </head>

    <body>
    <input type="button" value="Go" onclick="loopLinks(htmlArea.id, 3 , 'Page');">
    <p id="htmlArea">
    </p>
    </body>
    </html>

    Any help would be very much appreciated

  2. #2
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not exactly sure I understand what you want.

    I will deal with the following comment in your code:
    Code:
    // ERROR: "alert();" of the following, tries to link to "javascript:alert();" but if you attempt to put 
    //something in, like "alert('hi');" it tries to link to "javascript:alert(" and ignores the rest
    The second line of your comment happens because this is where the parser thinks the string terminates. If you want to display hi, you will need to escape the double quote characters around hi, i.e. try this:

    Code javascript:
    newLink(idP, "alert(\"hi\");", commonName + (i + 1));

    alternatively, you may wish to consider the following:
    • Define a new variable, let's say, x.
    • let x = "hi".


    then try
    Code javascript:
    newLink(idP, "alert(x);", commonName + (i + 1));

    It will also alert hi. x can have either global or local scope depending on what you need.

    I hope this helps.

    Regards,
    YMas

  3. #3
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i swear i already tried the \" thing, but it works this time. Thanks a lot.


Bookmarks

Posting Permissions

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