Generic New Window Script

Discussion in 'JavaScript' started by prowlie, Oct 1, 2006.

  1. #1
    Hi! to make this short, this is my problem, i have multiple links which open new windows on one page, this is an example of the code used to call the function for this action

    <div align="top">
    <a href="" onClick="Javascript:blogNewWindow(210906, 'Getting the gear in order');return false">
    <img src="blog/blog210906.jpg"><br><i>"Getting the gear in order"</i></a></div>
    Code (markup):
    This is the function (in an external script):

    function blogNewWindow(whichWindow, caption) {
    	blogNewWindow=window.open('','name','height=570, width=700, left=50,top=50,screenX=50,screenY=50')
    	var blogImageWindow = blogNewWindow.document
    	var blogWhichImage = '<img src="blog/blog'+whichWindow+'Big.jpg">'
    		
    	blogImageWindow.write('<html><head><title>Blog - Full Size Images</title>')
    	blogImageWindow.write('<link rel="stylesheet" href="portfolioStyleSheet2.css">')
    		
    	blogImageWindow.write(blogWhichImage)
    		
    	blogImageWindow.write('<p align="center">')
    	blogImageWindow.write('<font class="bodyText"><i>"')
    	blogImageWindow.write(caption)
    	blogImageWindow.write('" || </i></font>')
    		
    	blogImageWindow.write('<font class="bodyText"><a href="javascript:self.close()">close</a> this window.</font>')
    	blogImageWindow.write('</body></html>')
    	blogImageWindow.close()
    	}
    Code (markup):

    It all works fine, but for some reason it only works once, so, if you click on one link to open a new window it works, but if you click on another link to open another window (even when the first new window is closed) it doesnt work, yet, clicking on the link, again, makes it work, so essentially, you have to click twice on any 'new window' link after the first time to create the new window.

    Any thoughts?

    Cheers people :)
     
    prowlie, Oct 1, 2006 IP
  2. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes. Don't open new windows. :)

    It's simply a hindrance to users who actually know how to use their browser to do it, or prefer not to, or use tabs... the list goes on.
     
    penagate, Oct 2, 2006 IP
  3. BurgerKing

    BurgerKing Active Member

    Messages:
    397
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Hi Prowlie,

    The problem is that the variable that you assign the new window to 'blogNewWindow' is global.

    This means that the first time you run the function it works (because blogNewWindow is uninitialised). The second time you run it, it is already set to the first window.

    The easy way around this is to declare blogNewWindow as local to your function.

    Eg: This worked for me.

    function blogNewWindow(whichWindow, caption) {

    var blogNewWindow;

    blogNewWindow=window.open('','name','height=570, width=700, left=50,top=50,screenX=50,screenY=50')
    var blogImageWindow = blogNewWindow.document
    var blogWhichImage = '<img src="blog/blog'+whichWindow+'Big.jpg">'

    blogImageWindow.write('<html><head><title>Blog - Full Size Images</title>')
    blogImageWindow.write('<link rel="stylesheet" href="portfolioStyleSheet2.css">')

    blogImageWindow.write(blogWhichImage)

    blogImageWindow.write('<p align="center">')
    blogImageWindow.write('<font class="bodyText"><i>"')
    blogImageWindow.write(caption)
    blogImageWindow.write('" || </i></font>')

    blogImageWindow.write('<font class="bodyText"><a href="javascript:self.close()">close</a> this window.</font>')
    blogImageWindow.write('</body></html>')
    blogImageWindow.close()
    }

    Cheers.
     
    BurgerKing, Oct 2, 2006 IP
  4. prowlie

    prowlie Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for helping :D
     
    prowlie, Oct 5, 2006 IP