SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Evangelist kyllle's Avatar
    Join Date
    Jun 2008
    Posts
    469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Opening 2 individual pages using the same new window?

    Hi all,

    Basically I have created 3 pages - index.html, page1.html + page2.html

    I have 2 links in index.html that link off to page1.html + page2.html

    My question is, can I use javascript in such a way that if I click link 1 then page1.html opens in a new window but if I then click link 2 then page2.html appears in the window that currently has page1.html?

    If anyone can suggest what can be done would be great, here is a link to my page: XHTML5 Skel

    Thanks for your help!

    Kyle

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Simply specify a name for the window in the second parameter of the window.open() and they will all use the one window that has that name and only create a new window if a window with that name doesn't already exist (avoid using a name that starts with an underscore as some of those names have special meanings such as _blank meaning to always open a new window).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Evangelist kyllle's Avatar
    Join Date
    Jun 2008
    Posts
    469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Felgall,

    Thanks for the reply, Does this mean I will have to add the code directly into the anchor tags or can this be seperate from the html?

    Kyle

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kyllle View Post
    Does this mean I will have to add the code directly into the anchor tags or can this be seperate from the html?
    No, for F's sake no! Don't faff about with the HTML code.

    I see that you are already loading jQuery, so you can easily use that to achieve your needs.

    Code javascript:
    $('#container a').click(function (evt) {
        window.open(this, 'childWindow', 'width=600,height=400');
        evt.preventDefault();
    });

    The purpose of specifying a width and height, is to help encourage web browsers to not open the window as a new tab, but as a separate window instead.

    The preventDefault part prevents the parent window from performing its default action of opening up the new page in the parent window.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •