Running javascript through php on an iframe

Discussion in 'Web Design and Development' started by Macman1993, May 13, 2010.

  1. Macman1993 macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #1
    So like the title implies I am trying to use php to make a javascript link that changes the video playing in an html5 player to one of the file links stored in an array...if you haven't closed the topic at this point I'm surprised. I have had no trouble changing the file type to a set file using a button using html with this code

    Code:
    <input type="button" value="Switch" onClick="parent.video.mediaSwitch('file2.mp4')">
    
    Ok a small bit of info on my code, "video" is the name of the iframe that the html5 player, and mediaSwitch is a javascript function I store that will change the file that is playing to whatever is passed in as a parameter. What my final goal is to have a video player beside a small web app (this is meant for an ipad) and that web app will list all the files and if they are a .mp4 file clicking the link will change the video in the player to that video. I have everything working and I can display the files and do a raw link to them but when the php script generates the link for the file it doesn't execute the code. I use the following to try and generate the link.

    Code:
    if ($ext == "mp4") {
      	echo "<li class='menu'><a href='javascript: parent.video.mediaSwitch($files[$j])'>";
    }
    
    So for this code $ext is a working variable that holds the files extension, files[j] is stored inside the $files[] array and $j is just the int used in the for loop.

    When I load the page the script works without any errors but clicking the link only gets the above code repeated only with the variables replaced with the correct file. The only problem is the link so if anyone has an idea for what I can do it would be greatly appreciated.

    A screen shot of the script running locally (with the exception of the .php file which is delivered by a server) is attached in case anyone is interested/needs a visual representation to understand it better.
     

    Attached Files:

  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    I don't generally work with multiple windows as I find them annoying from a user perspective, especially since I run my browser in single window mode so I can't have two windows next to one another. My recommendation would be to have the separate window as a pop-over window that the user can drag around, but it would still be part of the page. You could use some JavaScript to hide it away when the user doesn't need it as well.

    As far as your current setup, do any error messages appear in the error console? Have you tried some debug messages to ensure that the parent object exist and other variables to see where the code may be failing? That would be my troubleshooting process.
     
  3. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #3
    I checked my error console (probably should have done that before) I got a SyntaxError: Parse error when clicking the links that run the script. I figured it would be a syntax error but the problem is I just have no idea what I should put in for the code to chance the video.

    Also this page is all running in one window, I use iframes to keep the content in place because with iframes I can move through the file browser without interrupting video playback.
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Well I parse error usually means like a missing parentheses or semicolon. Was that the full error message? You can use alert statements to find out if it doesn't like something in onclick area or if the issue is within the mediaSwitch function. Based on just the code you provided I don't see any syntax issues, but that doesn't mean the problem isn't in other code you haven't shown.
     
  5. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #5
    Ok heres the full error that I'm getting, for some reason two now show up.

    Code:
    Unsafe JavaScript attempt to access frame with URL http://clm51193.com/html5/index.html from frame with URL http://www.clm51193.com/24%20Season%206/. Domains, protocols and ports must match.
    SyntaxError: Parse error
    
     
  6. colocolo macrumors 6502

    Joined:
    Jan 17, 2002
    Location:
    Santiago, Chile
    #6
    It is pretty hard to determine the syntax error without taking a look at the actual code; however, that first error happens because javascript can't be called across different sites.
    In this case, one of your frames presents itself as http://clm51193.com/ and the other one as http://www.clm51193.com/

    Fix both sources so both say either http://clm51193.com/ or http://www.clm51193.com/ and that will solve itself.

    Because of safety reasons, almost no browser allows javascript to be called from one site to another. And with good reason :)
     
  7. Macman1993 thread starter macrumors 6502

    Macman1993

    Joined:
    Nov 23, 2007
    #7
    I'm confused, I understand what your saying about pages not being edited by external pages but what I'm not understanding is why the sidebar.php page cannot change the video in index.html. I just changed it for testing making sidebar.php into sidebar.html and all it has is a play/pause button and that works and since its an external page I don't understand why its any different when I add the .php extension.
     

Share This Page