Results 1 to 8 of 8

Thread: Adding Adding onclick=window.location within document.write tag

  1. #1
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding Adding onclick=window.location within document.write tag

    I am trying to put the following a href tag into a document.write tag and I can't seem to get the syntax right or know if it is possible to put another javascript function call into a different javascript:

    This is the tag that would rest in a lone href tag:
    <a href="#" onclick="window.location=y">Test</a>
    Here y is a javascript variable with some url.

    And this is how I tried to enter the tag:

    document.write("<a href=# onclick=window.location='+ y +'>Test</a>");

    Any help would be MUCH appreciated.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    document.write('<a href="#" onclick="window.location = ' + y + '; return false;">Test</a>');
    If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Did not work

    I tried your code John, but it dont seem to work well.

    Here is my code snippet:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <script type="text/javascript">
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","cd_catalog.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    
    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("CD");
    var y=x[0].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue;
    alert(y);
    for (i=0;i<x.length;i++)
      { 
      document.write("<tr><td>");
      document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
      document.write("</td><td>");
      document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
      document.write("</td></tr>");
      }
    document.write("</table>");
    document.write('<a href="#" onclick="window.location = ' + y + '; return false;">Test</a>');
    </script>
    
    </body>
    </html>
    I am basically trying to read the contents of an xml file and storing it in variable y. and using that as an href.

    When in a lone a href tag, it works fine and www.google.com opens up just fine, as the value of y is www.google.com, but the same fails in document.write.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Ooops, that should be:

    Code:
    document.write('<a href="#" onclick="window.location = \'' + y + '\'; return false;">Test</a>');
    However, you could get the same basic result like so:

    Code:
    document.write('<a href="' + y + '">Test</a>');
    The only difference being that the user would be able to see the href (in browsers that do this) in the browser's status bar or equivalent when hovering the link.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default It worked!!

    Thanks a lot John, your code works..

  6. #6
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    John, shouldn't that be (with a back slash):
    Code:
    document.write('<a href="' + y + '">Test<\/a>');
    ?
    ===
    Arie.

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by molendijk View Post
    John, shouldn't that be (with a back slash):
    Code:
    document.write('<a href="' + y + '">Test<\/a>');
    ?
    ===
    Arie.
    In my experience - only if the code is on the page, and only if it's causing HTML/XHTML validation errors, and only if you care about that.

    The only closing HTML tag as a string inside a script that absolutely needs to be escaped like that on the page is the closing </script> tag. I'm unsure if it needs to be in an external script or not. I just checked - Firefox, IE 9 in all its modes, Opera, Chrome don't mind:

    ext.js:

    Code:
    document.write('<script>alert("hi");</script>');
    ext_test.htm:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="ext.js"></script>
    </head>
    <body>
    
    </body>
    </html>
    However, in a sense you're right. It never hurts to escape those slashes and neglecting to do so can at times cause one sort of problem or another depending upon circumstances.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Quote Originally Posted by jscheuer1 View Post
    In my experience - only if the code is on the page, and only if it's causing HTML/XHTML validation errors, and only if you care about that.
    The only closing HTML tag as a string inside a script that absolutely needs to be escaped like that on the page is the closing </script> tag. I'm unsure if it needs to be in an external script or not. I just checked - Firefox, IE 9 in all its modes, Opera, Chrome don't mind
    Another good reason to use external scripts instead of internal ones.
    ===
    Arie.

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
  •