SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question JavaScript Code for New Window

    What is the JavaScript code to open a new window? For example, I have a form and in the <FORM> tag I want to put this:

    Code:
    <form method="POST" action="page.php" onclick="open new window code">
    Any help would be greatly appreciated!

  2. #2
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    target=new
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  3. #3
    SitePoint Zealot t0m|ta's Avatar
    Join Date
    Mar 2002
    Location
    Madrid - Spain
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a little scripts that lets you control how big you want the window, with scrolling, without it, etc. It's not mine, I saw it at a site I was writing content for ages ago and does not have any copyright info so I do not know who devoloped it originally :-(

    You need

    Put this either in the <head> section of your page (with <script> tags) or in a separate .js file
    NOTE: could not find a [code] tag :-)

    var win=null;
    function NewWindow(mypage,myname,w,h,scroll,pos){
    if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
    if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
    else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
    settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
    win=window.open(mypage,myname,settings);}
    // -->



    Example of use:

    <a href="Whatever html" title="whatever explanation" onClick="NewWindow(this.href,'detalle','650','450','yes','center');return false" onFocus="this.blur()">Chat</a>

    or a tutorial on manipulating windows with javascript: http://hotwired.lycos.com/webmonkey/98/03/index3a.html

    I hope this helps you :-)
    Last edited by t0m|ta; Aug 13, 2002 at 00:51.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried each one of them and got different syntax errors with each.

    Code:
    onclick="target=new"
    Results in a syntax error.

  5. #5
    SitePoint Zealot t0m|ta's Avatar
    Join Date
    Mar 2002
    Location
    Madrid - Spain
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmmm

    in html <a href="link.html" target="_blank">Whatever</a>

    In Javascript:

    onClick=window.open("URL","name","features");

    Ejample:

    <a href="#" onClick="window.open('javascript_window_1.html','javascript_1'); return false;">Here's a window named javascript_1</a>.
    When you click on this link, a


    Courtesy: Thau's Javascript tutorial: http://hotwired.lycos.com/webmonkey/...x3a_page4.html

    ;-)

  6. #6
    SitePoint Guru
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out my very first post. I'm not using the onclick in a <a> tag, I'm using it in a <FORM> tag.

  7. #7
    SitePoint Zealot t0m|ta's Avatar
    Join Date
    Mar 2002
    Location
    Madrid - Spain
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mm your right. I'm a complete idiot. :-(

    Try the code with the form, but on a button, it should work on a button I think, I'll give it a try tomorrow, it's very very late for me now :-)

    Curiosity killed the cat, both codes work one is an onsumit event on the form, and the second is a onclik event on the button:

    <form name="form1" method="post" action="">
    <input type="submit" name="Submit" value="OnSubmit" onclick="window.open('http://www.page.ws','javascript_1'); ">
    <input type="text" name="textfield">
    </form>


    <form name="form1" method="post" action="" onsubmit="window.open('http://www.tomatoma.ws','javascript_1'); ">
    <input type="submit" name="Submit" value="OnSubmit">
    <input type="text" name="textfield2">
    </form>
    Last edited by t0m|ta; Aug 13, 2002 at 21:40.

  8. #8
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Darin
    I tried each one of them and got different syntax errors with each.

    onclick="target=new"

    Results in a syntax error.
    You don't need onclick at all.

    As spartan mentioned, all you need is to specify the target.

    Simply add target="new" to the opening <form> tag.

    Code:
    <form method="POST" action="page.php" target="new">

    p.s.
    Originally posted by t0m|ta
    Here is a little script...
    Blimey, if that's what you call "little" I'd hate to see what you'd consider to be large.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature

  9. #9
    SitePoint Guru
    Join Date
    Sep 2001
    Location
    Vancouver
    Posts
    809
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!

  10. #10
    SitePoint Zealot t0m|ta's Avatar
    Join Date
    Mar 2002
    Location
    Madrid - Spain
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mmm jeje bill it is small :P.

    See I love that piece of code :-) It gives me absolute control of the popup window, while letting users open their own window if they want.

    I hate it when I right click on link, choose to open in new window, and get a horrible blank page with javascript; as the url.

    This one does not do that, if you right click the new window opens, atributes like position are lost but the link continues to worK

    I don't know how it works, I just know it does what i want :-)

  11. #11
    SitePoint Member
    Join Date
    May 2002
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I'd like to split this conversation right down the middle.
    I want to use the "target=new"..Is there anyway to control the window size and location???
    Thanks

  12. #12
    SitePoint Wizard Bill Posters's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    1,523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Grizz2
    I want to use the "target=new"..Is there anyway to control the window size and location???
    You cannot specify those things by simply using target="new" on its own.

    To stick with target="new" and still have the next page positioned and sized to your requirements you could use javascript in the header of the target page.

    Using...
    Code:
    <script type="text/javascript">
    
    window.moveTo(x,y);
    window.resizeTo(w,h)
    
    </script>
    ...in the header of a page will (as it suggests) reposition and resize the window to whatever position and size you specify.

    To specify the window position *and* size from the creating page you will still need to use the javascript window.open() method.
    New Plastic Arts: Visual Communication | DesignateOnline

    Mate went to NY and all he got me was this lousy signature


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
  •