Web Analytics Made Easy -
StatCounter Open a New Window - CodingForum

Announcement

Collapse
No announcement yet.

Open a New Window

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

  • Open a New Window

    when a user click on a link, it would open a new window in HTML it is something like this:
    Code:
    <a href="http://google.com" target="_blank">click here</a>

    How do I do this in Javascript? So a user would click on the link and it would open a new window. Here's my code
    Code:
    document.writeln('<tr><td>' + "http://google.com" + '</td></tr>');
    I know window.open(url) would open a new window, but how do I make it clickable??

    Any comments or suggestion would be greatly appreciated.
    thanks
    Daily Hot Deal

  • #2
    http://www.pageresource.com/jscript/jwinopen.htm

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Originally posted by hiyatran View Post
      I know window.open(url) would open a new window, but how do I make it clickable??
      Is this what you mean?

      Code:
      <a href="" onclick="window.open('someURL'); return false;">Click to open new window</a>
      you can add extra parameters to window.open to size the window etc etc etc

      Comment


      • #4
        I would like to put the link in a javascript and NOT HTML, like so

        document.writeln('<tr><td onclick="window.open('http://www.google.com');">Click Here</td></tr>');

        but doesn't work!!
        Daily Hot Deal

        Comment


        • #5
          Originally posted by hiyatran View Post
          I would like to put the link in a javascript and NOT HTML, like so

          document.writeln('<tr><td onclick="window.open('http://www.google.com');">Click Here</td></tr>');

          but doesn't work!!
          Why? What is the point?

          Code:
          <table>
          <script type = "text/javascript">
          document.writeln('<tr><td> <a href = "" onclick="window.open(\'http://www.google.com\');">Click Here</td></tr></a>');
          </script>
          </table>

          Note the document.writeln that runs after the page finishes loading will create a new page and overwrite all of the content of the current page.

          All the code given in this post has been tested and is intended to address the question asked.
          Unless stated otherwise it is not just a demonstration.

          Comment


          • #6
            @op - it better to use dom methods (createElement(),appendChild() etc) to create elements and assign event handler functions rather than document.write or innerHTML as some people also do.

            Originally posted by Philip M View Post
            Code:
            document.writeln('<tr><td> <a href = "" onclick="window.open(\'http://www.google.com\'); return false;">Click Here</td></tr></a>');
            If you don't want to lose the contents of the parent window due to the blank href you will need to add return false; to cancel the default action of the href.

            Comment

            Working...
            X