SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help With A Popup Window

    I have a popup window that loads on a particular page of my site. The entire popup is a link that loads the page in the main window of my site. For some reason it comes up with a script error if I click the link after I close the main window. Can somebody tell me how I can make it so it will load a new browser window if the main window has been closed or cannot be found to prevent the error from coming up?

    Here is the code I'm using in my link in the popup:

    <a href="javascript:opener.document.location='main.html';window.close()"><img src="images/image.jpg" width="300" height="200" border="0"></a>
    John Saunders

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    John,

    My JavaScript might be a bit rusty, but I am pretty sure using "opener" is what is causing your script error. You are trying to load a document in the "opener" and that window has been closed, so an error is thrown.

    Without providing the code (As I said, I am a bit rusty! )... I will give you a tip...

    You have to do a check for the "opener" and then if it doesn't exist, open a new window. If you set it up strictly to open a new window and the opening window hasn't been closed, you'll have 2 windows instead of the intended 1.

    I am sure someone who does JavaScript on regular basis could hack that out quicker for you than me researching it!

    -freelancerx

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it
    Code:
    <script>
    function doLink(nUrl) {
    	if (top.opener)
    		top.opener.location.href = nUrl;
    	else
    		window.open(nUrl,'_blank');
    	top.close();
    	}
    </script>
    
    <a href="main.html" onClick="doLink(this.href); return false;"><img src="images/image.jpg" width="300" height="200" border="0"></a>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    beetle,

    Thanks for your help. I tried out your script and for some reason if the main browser window is closed and the link in the popup window is clicked, the page is loaded in the popup instead of a new browser window.

    Any ideas?
    John Saunders

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tell me what you get when you alert top.opener after the main window has been closed....
    Code:
    function doLink(nUrl) {
    	alert(top.opener)
    	if (top.opener)
    		top.opener.location.href = nUrl;
    	else
    		window.open(nUrl,'_blank');
    	top.close();
    	}
    We may need to name the opener window for this check to work....
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    Git-R-Done
    Join Date
    Nov 2001
    Posts
    1,194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    beetle,

    When I click on the popup link after the main window has already been closed, it comes up with a js popup that says [object] after the html popup window closes, just as it does when I click on it with the main window open.
    John Saunders


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
  •