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 14 of 14
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pop up check / 3 conditions (help)

    Before I begin I have just registered here, im not a massive javascript programmer and i need to know if the route im taking / what im asking for is feasable & just how it should be layed out in JS.

    Ive been all over the net getting nuggets of info but im still sketchy so I caved and logged in here to ask for something specific ... so here goes :

    I have a music player that pops up into a sized window that comes into focus when a user clicks anywhere on the "listen" html link (pop up window refreshes / or opens up and comes into focus, nice and simple.)

    The player in this pop up has 3 different playlists, so theres 3 different types of content loading into this one pop up depending on the html link.

    Im trying to avoid the same playlist / same content loading over and over / keep & refreshing if a user clicks a link that opens it (and its already open, which would interupt music playing.

    I need :

    When a link is clicked to open the pop up it:

    A: Checks its open (if not simply open and bring into focus)

    B: If it is open, check whats the content ... "if" the content = what its already trying to open in this pop up - dont refresh, simply bring into focus. IE: do nothing as its already open & content is the same so bring into focus only.

    C: If it is already open and the content is not whats its trying to open in that popup, go ahead and load it in and bring into focus.

    I get lost as I can only see you check via the window name, but, in my example thats not enough. I need an identifier more than just the window name. So whats happening in "B" is in some way checking the window name + a variable that identifies its either 1 of 3 types of content.

    So if it = itself just bring window into focus nothing more, if it doesnt = itself then load and bring to focus, if window is closed load and bring to focus.

    I want it all in 1 popup because its a music player and multiple pop ups would be very messy.

    Can this be done? Can somebody give me pointer / script examples of this working?

    So far I have this function on my site:


    function newWindow(newContent)
    {
    winContent = window.open(newContent, 'player', 'right=0,top=20,width=340,height=260,scrollbars=no')
    winContent.focus()
    }


    HTML:

    a href="javascript:newWindow('mediaplayer.php?set=2')"


    (set=2 is a variable to ask the player to load playlist 2 / 1 / or 3 as I have 3 playlist).


    Thanks in advance to anyone who can point me in the right direction .

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know if this does everything you want but it has some ideas in it.

    http://www.exfer.net/test/popup_win_test.html

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title>Popup Win Test</title>
        <script type="text/javascript">
          var winContent = null;
    
          function newWindow(newContent) {
            if (winContent == null) {
              winContent = window.open(newContent, 'player', 'right=0,top=20,width=340,height=260,scrollbars=no')
            } else if (winContent.location != newContent) {
              winContent.location = newContent;
            }
            winContent.focus()
          }
        </script>
      </head>
      <body>
        <div>
          <a href="javascript:newWindow('http://www.exfer.net/test/css_mid_test.html');">NewWin1</a>
          <a href="javascript:newWindow('http://www.exfer.net/test/test_is_letter.html');">NewWin2</a>
        </div>
      </body>
    </html>
    david_kw

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks David .. thats exactly what I required

    .. runs off to nail a problem that will end all my web problems (for now)

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Problem .........

    This code is fine once a pop up exists .. if you close the popup and click the links it errors out (I assume becuase its not checking to see if the window is closed) what can be done / is used to modify this to also check if its closed to open it so you dont get an error?

    (sorry to ask to be spoon fed but im crappy at syntax).

    ** how comes this works off that link, but when I copy the script to my own page local to test It doesnt work (pops up a window, anyclicks after that I get errors) ? I cannot test this local .. Ive copied the script to a blank html doc and put it on my server and it wont work like the linked version of yours ??

    Basicly after i click one of the links it will popup the window, once the pop up is open and I click on any of the 2 links to test I get an error on page, and its : "Permission Denied". Even though in the same browser I open your link and it works ok (albeit not working once you close the pop up from your link) ... im a tad confused.
    Last edited by Barriestard; 12-10-2006 at 04:20 PM.

  • #5
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I put a new version up. I forgot to test winContent.closed to make sure the window was still up. So the first test now looks like this

    if (winContent == null || winContent.closed) {

    As for the permission issue the reason is because of the security model. Browsers require a "same origin" to be able to check on child windows. If it isn't the same origin then you can't check things like winContent.property and it throws an exception. To make it work on your server, just have to links in the html <a> tags be changed to point to something on your own server and it should work.

    david_kw

  • #6
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    OK Ignore my last post.. mass confusion on my part.

    [LINK REMOVED]

    Heres your same javascript code in my type of setup, as you can see it still refreshes the window as it opens the same content (html pages are same content but names test 1 / test 2 and named as such "test1.html" / "test2.html").

    Why is this refreshing and yet its the same JS script you passed me from your link? As far as im concerned this mirrors yours so it should work?

    My html is:


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Popup Win Test</title>
    <script type="text/javascript">
    var winContent = null;

    function newWindow(newContent) {
    if (winContent == null || winContent.closed) {
    winContent = window.open(newContent, 'player', 'right=0,top=20,width=340,height=260,scrollbars=no')
    } else if (winContent.location != newContent) {
    winContent.location = newContent;
    }
    winContent.focus()
    }
    </script>
    </head>
    <body>
    <div>
    <a href="javascript:newWindow('test1.html');">NewWin1</a>
    <a href="javascript:newWindow('test2.html');">NewWin2</a>
    </div>
    </body>
    </html>
    Last edited by Barriestard; 12-11-2006 at 01:36 PM. Reason: URL removed as removing content.

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Now thats some sweet code

    I tested it out and its great. The thing is the function of using embedded media layer I dont want, im happy with the flash player im using currently. Although yet again amongst your script the focus / do-not-refresh-as-same-content-open part works ... so keeping it on topic (if you will) .. with what I already have can you or David give me a pointer to just getting past my final little hurdal.

    Which is ... why this code although it works on Davids link :

    http://www.exfer.net/test/popup_win_test.html

    .. doesnt work on Mine?

    (IE: his doesnt refresh if content already open and mine ignores that fact and refreshes anyway, but its the same code??) If I can nail this little part thats realy all I need, from that Im sorted (although the script for you media player aproach I will use later on ).

    Or ... from that media player type script, what can I whittle it down to so it fits what I need from Davids script? Im so near but im just lost as to why repeatig what david has passed me (and big thanks as its the direct answer to what i need) just wont work ... im lost? What is it im missing?
    Last edited by Barriestard; 12-11-2006 at 01:37 PM.

  • #8
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Ancora View Post
    I'm not "his." I have a name and expect to be addressed by it.
    Code DELETED.
    ??????????????? ... I know you have a name, I think your script is excellent, I refered once to youself as "you" because I was posting direct to "you" not david to keep things clear, thats all ... so whats offended you? Your helping right? I opened a thread to ask about some code that David kindly posted that works exactly as I was asking for, apart from a small niggle at my end when I use it. Your code has part of that element of what I was looking/asking for but overall it takes things to another direction of what I didnt ask for originaly (but its superb if I need to go the media player route). Im not the best scripter at all so I try to keep it in one area so I dont confuse myself. What you actualy posted was superb.

    To offend without even trying is beyond me ... dear me .

    Well David if your out there and can help I would love to know why my version (albeit the same) doesnt seem to want to play ball.
    Last edited by Barriestard; 12-10-2006 at 07:02 PM.

  • #9
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem is in the test.

    } else if (winContent.location != newContent) {

    Because in your case

    newContent = 'test1.html';
    winContent.location = 'http://www.theprojex.co.uk/test/test1.html';

    There are different ways to try and solve it. The easiest (but not necessarily so flexible) is

    <a href="javascript:newWindow(http://www.theprojex.co.uk/test/test1.html');">NewWin1</a>
    <a href="javascript:newWindow('http://www.theprojex.co.uk/test/test2.html');">NewWin2</a>

    Or you could recreate the path in the newWindow function by looking at window.location and creating the URL from a relative basis for the newContent so the test would succeed.

    david_kw

  • #10
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Ah, ok David ... the penny has dropped to use whole URL's not just assume it till look at the root

    Sorry for asking silly questions on this & thanks again for being so helpfull

    @ Ancora / Mike ... sorry to offend (not sure how) but the main thing is thanks for your input, your code is deffinately something to build on

  • #11
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Its been a while since using this code (great and works fine Dave thanks). Although I use this on a .com and also have a .co.uk. My problem is relative & absolute issues, using this code is fine direct from the .com but from .co.uk it wont work (as im using absolutes to ref the window thats popping up), .co.uk is pointing to the the same space as the default .com BTW.

    An example in my html using this code is :

    <a href="javascript:newWindow2('http://www.(URL).com/mediaplayer.php?set=3')" I realy need it to be :

    <a href="javascript:newWindow2('mediaplayer.php?set=3')" <--- this is what I need but then the refesh checking part then fails.

    I need this to work from a .com & .co.uk fully, so far If I make links relative, the part of the code that checks if the content already open is the same (and then wont refresh) fails, so I have to make it absolute .. thus having issues from .co.uk as im using .com in the path to open the window. I have had some feedback which doesnt work correct, but asks better what Im trying to to rework from Daves code :

    It's this bit of your script that's the problem:-

    } else if (win2Content.location != newContent) {
    win2Content.location = newContent;
    }

    win2Content.location is always set to the absolute path when a window is created, even if it was created from a relative path, but newContent holds only the relative path so they don't match (even though you know they mean the same thing) and therefore the line below (win2Content.location = newContent reloads the page even though it doesn't have to.

    This means that the comparison logic needs to be a bit smarter and use the IndexOf function to check for an occurence of newContent in win2Content.location rather than comparing them. I think this will do the trick:-

    } else if (win2Content.location.IndexOf(newContent) = -1) {
    win2Content.location = newContent;
    }

    I haven't tested this bit of code so it might not work exactly as I've written it (especially if the location object isn't a string object) but basically, you're looking to see if the string contained in newContent is part of the string contained in win2Content.location. If it's not, the IndexOf function returns -1 and your script loads a new page.
    This is Daves script with that small change that doesnt work ATM:

    var win2Content = null;

    function newWindow2(newContent) {
    if (win2Content == null || win2Content.closed) {
    win2Content = window.open(newContent, 'player', 'left=400,top=285,width=335,height=275,screenX=400,screenY=400,resizable=no,scrollbars=no,toolbar=no ,location=no,directories=no,status=no,menubar=no')
    } else if (win2Content.location.IndexOf(newContent) = -1) {
    win2Content.location = newContent;
    }

    win2Content.focus()
    }
    This doesnt work but is on the right track, can anyone help on how this part of the code would need to changed in order for this to work correctly, so that I can use relative paths & then .co.uk or .com would work fine.

    I hope ive explained myself well enough

    Dave did state this earlier which I think is the way I need to go:

    Or you could recreate the path in the newWindow function by looking at window.location and creating the URL from a relative basis for the newContent so the test would succeed.

    david_kw
    SOrry to be spoon fed but I am no coder and not the best at this, any help is welcomed, thanks.
    Last edited by Barriestard; 01-30-2007 at 11:30 AM.

  • #12
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Before I look too closely at it I do see one bug in your change.

    } else if (win2Content.location.IndexOf(newContent) = -1) {

    should be

    } else if (win2Content.location.IndexOf(newContent) == -1) {

    with the extra equal sign. Hopefully that's the only problem. :)

    david_kw

  • #13
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Dave,

    Dear me that realy threw a red herring into the works. Ok I fixed that oversight, the java page error was:

    Wrong number or arguments or invalid propery assignment
    So now thats been fixed, its still not working correctly, from either .com or .co.uk set relative.

    <a href="javascript:newWindow2('mediaplayer.php?set=3')"
    this is the relative link also with tweaked with the "==" fix,when the window is already open and its runs the "else if" part I get this error:

    Object doesnt support this propery or method
    this is the same error with that syntax fix to the else/if code, and also whether or not the html link isrelative or absolute, and whether or not your at .com or .co.uk.

    So currently that change is worse than just having absolute & only having .co.uk problems (which is how it is live until I see the wood from the trees).

    Im not sure at all myself what way to tackle it to fix ... Im out of juice as my knowledge isnt all that.

    Apart from killing the .co.uk and just using .com (which is a bit of a poor workaround) what could be used as a cure to make sure it runs the else/if part without errors and works with relative links thus working from .com or .co uk?

    Any input at this stage is gold, because im slightly out of my depth with this & it a realy niggly issue I would love to nail & learn from.
    Last edited by Barriestard; 01-31-2007 at 04:28 PM.

  • #14
    New Coder
    Join Date
    Dec 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any fruther input or is this idea exhausted do you think?


  •  

    Posting Permissions

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