Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    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.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote 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

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    39
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!!

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote 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.

  • #6
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    @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.

    Quote 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.


  •  

    Posting Permissions

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